我试图在Go程序中使用外部C库。Cgo包含档案.a文件
我曾尝试以下:
package cgoexample
/*
#include <stdio.h>
#include <stdlib.h>
#cgo CFLAGS: -I/Users/me/somelib/include
#cgo LDFLAGS: /Users/me/somelib/libhello.a
#include "stinger.h"
void myprint(char* s) {
printf("%s", s);
}
*/
import "C"
import "unsafe"
//... more here
在/Users/me/somelib/include
有.h文件和在libhello.a
存在的.o文件(I检查使用ar
命令),具有定义的功能是在.h文件中。
似乎.h文件链接正常,但它看起来不像档案文件正在链接。我不断收到这些:
warning: 'some_method_in_my_h_file" declared 'static' but never defined
而且这些警告被视为错误。无论如何,他们应该在档案文件中实施,所以我很困惑我在这里做错了什么。我运行go build
和gun run
。
我有一种感觉,我#cgo
命令是无效的(我不是C专家),
你可以添加函数定义/声明吗?如果声明的声明是静态的,它们应该在声明的文件中定义和使用。函数定义中的'static'告诉编译器不要将函数导出到文件外部。 – tomahh