当你使用cgo时,为什么会报错”undefined reference to `add2″?

huangapple go评论78阅读模式
英文:

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 &quot;Test.h&quot;
*/
import &quot;C&quot;
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&#39;:
rocketmq-bridge/src/cgo-gcc-prolog:42: undefined reference to
add2'
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

huangapple
  • 本文由 发表于 2017年6月22日 16:15:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/44693846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定