dongqian3750 2019-09-15 13:56
浏览 585

从Visual Studio 2019中的C项目访问Golang代码

I have a C project in Visual Studio 2019 and I would like to use certain functions written in Go within it.

I have a test file called ctest.go which has the following contents:

package main

import (
    "C"
    "fmt"
)

//export printInt
func printInt(x int) {
    fmt.Printf("Hello from go: %d", x)
}

func main() {}

I'm generating a static library with this command:

go build -buildmode=c-archive ctest.go

I end up with two files, ctest.a and ctest.h which I include in my VS project. I include ctest.h in my code and immediately get these errors:

identifier "__SIZE_TYPE__" is undefined
expected a ';'
expected a ';'

The generated header file contains the following lines which caused it:

typedef __SIZE_TYPE__ GoUintptr;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

I replaced __SIZE_TYPE__ by size_t and just commented out the _Complex lines to stop the errors.

Now my VS project compiles while including the header file. But as soon as I try to use my go function like this:

printInt(5);

I get 2 warnings followed by 6 errors:

Warning LNK4078 multiple '.text' sections found with different attributes (60600060)    AWO F:\AWO\AWO\ctest.a(go.o)
Warning LNK4078 multiple '.text' sections found with different attributes (60600060)    AWO F:\AWO\AWO\ctest.a(go.o)
Error   LNK2019 unresolved external symbol __imp___iob referenced in function __cgo_preinit_init    AWO F:\AWO\AWO\ctest.a(000005.o)
Error   LNK2001 unresolved external symbol __imp___iob  AWO F:\AWO\AWO\ctest.a(000006.o)
Error   LNK2001 unresolved external symbol __imp___iob  AWO F:\AWO\AWO\ctest.a(000007.o)
Error   LNK2019 unresolved external symbol _fprintf referenced in function _x_cgo_sys_thread_create AWO F:\AWO\AWO\ctest.a(000005.o)
Error   LNK2001 unresolved external symbol _fprintf AWO F:\AWO\AWO\ctest.a(000007.o)
Error   LNK1120 2 unresolved externals  AWO F:\AWO\Debug\AWO.exe    1   

This is where I'm stuck. I'm not sure what I'm doing wrong or should be doing differently to properly use the library generated with go.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Pwm双极模式H桥驱动控制电机
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题