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 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序