How to use C library in Golang(v1.3.2)

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

How to use C library in Golang(v1.3.2)

问题

这是我的Go源代码:

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
}

facerec_lib.h

int sumUp(int a, int b);

facerec_lib.c

/* 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$

我该如何处理这个问题?非常感谢!

编辑

我将facerec_lib.h更改为facerec_lib.c,问题解决了。我想我可能在Go文件中漏掉了一些FLAGS,有什么提示吗?

英文:

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
}

facerec_lib.h

int sumUp(int a, int b);

facerec_lib.c

/* 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 !

EDIT

I changed facerec_lib.h to facerec_lib.c, problem solved, I thinks I must miss some FLAGS in go file, any hints ?

答案1

得分: 9

如果你通过cgo使用一个C函数,你必须确保该函数的实现已经链接到你的Go包中。根据你问题的评论,似乎并非如此。

有两种方法可以解决这个问题:

  1. 在你的包目录中放置一个.c文件来实现该函数。当你运行go build时,它将与Go代码一起编译和链接到你的包中。

  2. 如果该函数是在一个库中实现的,你可以在Go文件中的import "C"语句之前的注释中使用LDFLAGS指令。例如:

     // #cgo LDFLAGS: -lmylib
     import "C"
    
英文:

If you make use of a C function via cgo, you must make sure that the implementation of that function is linked into your Go package. From the comments on your question, it seems this was not the case.

Two ways you can go about this include:

  1. place a .c file in your package's directory that implements the function. When you run go build, it will be compiled and linked into your package along with the Go code.

  2. If the function is implemented in a library, you can use the LDFLAGS directive in the comment before an import "C" statement in a Go file. For example:

     // #cgo LDFLAGS: -lmylib
     import "C"
    

huangapple
  • 本文由 发表于 2015年8月7日 09:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/31868482.html
匿名

发表评论

匿名网友

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

确定