Gccgo在go中传递和修改C结构时出错
我试图将一个go库转换为由C调用的库,我从一个简单的测试(基于我发现的有效文件)开始,但当我尝试构建时,会出现错误。我尝试将C中的一个结构传递给一个go函数,该函数使用该结构中的数据并返回一个值(在本例中是一个整数,但最终它将是另一个结构) Go版本:1.4.2 gccgo版本:5.0.1 gcc版本:4.9.2 h型Gccgo在go中传递和修改C结构时出错,c,go,cgo,gccgo,C,Go,Cgo,Gccgo,我试图将一个go库转换为由C调用的库,我从一个简单的测试(基于我发现的有效文件)开始,但当我尝试构建时,会出现错误。我尝试将C中的一个结构传递给一个go函数,该函数使用该结构中的数据并返回一个值(在本例中是一个整数,但最终它将是另一个结构) Go版本:1.4.2 gccgo版本:5.0.1 gcc版本:4.9.2 h型 typedef struct num { int n; } num; foo.go package main // #include "type.h" import
typedef struct num {
int n;
} num;
foo.go
package main
// #include "type.h"
import "C"
func New(x int) C.struct_num {
num := C.struct_num{}
num.n = C.int(x)
return num
}
func Add(num C.struct_num, x int) int {
return int(num.n) + x
}
酒吧c
当我运行make时,我得到错误信息:
gcc foo.o bar.c -o main
foo.o:(.rodata.__go_td_pN23_example.main._Ctype_int[__go_td_pN23_example.main._Ctype_int]+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_td_pN23_example.main._Ctype_int[__go_td_pN23_example.main._Ctype_int]+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_int+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_int+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_td_pN30_example.main._Ctype_struct_num[__go_td_pN30_example.main._Ctype_struct_num]+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_td_pN30_example.main._Ctype_struct_num[__go_td_pN30_example.main._Ctype_struct_num]+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_struct_num+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_struct_num+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_td_pN24_example.main._Ctype_void[__go_td_pN24_example.main._Ctype_void]+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_td_pN24_example.main._Ctype_void[__go_td_pN24_example.main._Ctype_void]+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_void+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_void+0x20): undefined reference to `__go_type_equal_identity'
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'main' failed
make: *** [main] Error 1
根据我从其他搜索中了解到的情况,这些错误与对C go库的调用有关,表明这可能没有被编译到对象文件中。我也尝试过在make文件中直接运行gccgo,结果相同。感谢您的任何见解和/或帮助。谢谢
编辑
多亏了你,我才明白这一点。显然,我需要在Makefile中为main调用gccgo,因为gcc不知道如何正确链接go运行时。从未使用过gccgo。有没有更好/不同的方法来编译go模块以链接到C项目中?据我所知,使用go命令倾向于只编译可运行的二进制文件,其中go是程序的入口点并运行C代码,而不是相反。我读过其他(旧)帖子,提到go需要go运行时,但显然在使用go构建时必须将其打包到二进制文件中,因为它可以在没有安装go的相同体系结构的机器上运行,所以至少在这种情况下,必须在二进制文件中包含必要的运行时组件。从未使用过gccgo。有没有更好/不同的方法来编译go模块以链接到C项目中?据我所知,使用go命令倾向于只编译可运行的二进制文件,其中go是程序的入口点并运行C代码,而不是相反。我读过其他(旧的)帖子,提到go需要go运行时,但显然,在使用go构建时,这必须打包到二进制文件中,因为它可以在没有安装go的相同体系结构的机器上运行,所以至少在这个用例中,二进制文件中必须包含必要的运行时组件。
all: main
main: foo.o bar.c
gcc foo.o bar.c -o main
foo.o: foo.go
go build -compiler gccgo -gccgoflags '-c -o foo.o -fgo-prefix=example' foo.go
clean:
rm -f main *.o
gcc foo.o bar.c -o main
foo.o:(.rodata.__go_td_pN23_example.main._Ctype_int[__go_td_pN23_example.main._Ctype_int]+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_td_pN23_example.main._Ctype_int[__go_td_pN23_example.main._Ctype_int]+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_int+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_int+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_td_pN30_example.main._Ctype_struct_num[__go_td_pN30_example.main._Ctype_struct_num]+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_td_pN30_example.main._Ctype_struct_num[__go_td_pN30_example.main._Ctype_struct_num]+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_struct_num+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_struct_num+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_td_pN24_example.main._Ctype_void[__go_td_pN24_example.main._Ctype_void]+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_td_pN24_example.main._Ctype_void[__go_td_pN24_example.main._Ctype_void]+0x20): undefined reference to `__go_type_equal_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_void+0x18): undefined reference to `__go_type_hash_identity'
foo.o:(.rodata.__go_tdn_example.main..example_main._Ctype_void+0x20): undefined reference to `__go_type_equal_identity'
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'main' failed
make: *** [main] Error 1