This is my Go source :
package facerec
/*
#include "libs/facerec_lib.h"
*/
import "C"
// import "unsafe"
type FaceRecServiceImpl struct {
}
func (this *FaceRecServiceImpl) Compare(features1 []byte, features2 []byte) (r float64, err error) {
// TODO
result := C.sumUp(2, 3)
return float64(result), nil
}
int sumUp(int a, int b);
/* File facerec.c */
#include "facerec_lib.h"
int sumUp(int a, int b)
{
return a + b;
}
go build:
Roy-MacBook-Air:facerec $ go build
# xxx/facerec
Undefined symbols for architecture x86_64:
"_sumUp", referenced from:
__cgo_35aa8b5c98e0_Cfunc_sumUp in face_rec.cgo2.o
(maybe you meant: __cgo_35aa8b5c98e0_Cfunc_sumUp)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Roy-MacBook-Air:facerec$
How can I deal with this issue ? Thanks a lot !
I changed facerec_lib.h
to facerec_lib.c
, problem solved, I thinks I must miss some FLAGS
in go file, any hints ?
答案 0 :(得分:5)
如果您通过cgo使用C函数,则必须确保该函数的实现链接到您的Go包中。从您对问题的评论来看,情况似乎并非如此。
您可以采取的两种方式包括:
将.c
文件放在包的实现该功能的目录中。当您运行go build
时,它将被编译并链接到您的包中以及Go代码。
如果函数在库中实现,则可以在Go文件中的import "C"
语句之前的注释中使用the LDFLAGS directive。例如:
// #cgo LDFLAGS: -lmylib
import "C"