The directory is follow:
-inlcude
Test.h
-lib
libmytest.so
-src
test.go
The test.go code is follow:
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': rocketmq-bridge/src/cgo-gcc-prolog:42: undefined reference to
add2' collect2: error: ld returned 1 exit status
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