英文:
why report the error " undefined reference to `add2" when I used cgo
问题
目录如下:
- include
- Test.h
- lib
- libmytest.so
- src
- test.go
test.go代码如下:
package main
/*
#cgo CFLAGS : -I ../include
#cgo LDFLAGS: -L ../lib -lmytest
#include "Test.h"
*/
import "C"
func main() {
C.add2(10, 10)
}
当我使用go build test.go
时,控制台报告如下错误:# command-line-arguments
/tmp/go-build168903458/command-line-arguments/_obj/test.cgo2.o: 在函数 _cgo_9efddd4c1a4b_Cfunc_add2' 中: rocketmq-bridge/src/cgo-gcc-prolog:42: 对
add2' 的引用未定义
collect2: error: ld 返回了 1 个退出状态
英文:
The directory is follow:<br>
-inlcude<br>
Test.h<Br>
-lib<br>
libmytest.so<br>
-src<br>
test.go<Br>
The test.go code is follow:<br>
package main
/*
#cgo CFLAGS : -I ../include
#cgo LDFLAGS: -L ../lib -lmytest
#include "Test.h"
*/
import "C"
func main() {
C.add2(10, 10)
}
When I use go build test.go
, the console report that: # command-line-arguments
/tmp/go-build168903458/command-line-arguments/_obj/test.cgo2.o: In function _cgo_9efddd4c1a4b_Cfunc_add2':
add2'
rocketmq-bridge/src/cgo-gcc-prolog:42: undefined reference to
collect2: error: ld returned 1 exit status
答案1
得分: 1
请不要在CFLAGS
后面添加空格,并且在构建cgo
时尝试不指定文件名,即:
go build
可选地,可以在构建选项中添加-x
,然后观察编译器是否正确地将相应的文件(INCLUDE和LIB)添加到其选项中,即:
go build -x
英文:
Do not add space after CFLAGS
, and try without specifying the filename when building cgo
, i.e.
go build
Optionally, add -x
to the build options, then observe whether the compiler correctly add the corresponding files (INCLUDE and LIB) to its option, i.e.
go build -x
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论