这可能吗?
我想从 C 程序调用以下 Go 函数:
// package name: test
package main
import "C"
//export Start
func Start() {
println("Hello world")
}
func main() {
}
我正在使用以下命令来构建存档
go build -buildmode=c-archive -o test.a main.go
使用 gcc 我可以让这个 C 程序运行:
#include <stdio.h>
#include "test.h"
int main() {
Start();
return 0;
}
我正在使用以下命令来构建可执行文件:
gcc main.c sdlgotest.a -o main -lpthread
这一切都适用于 amd64,但我想在 aarch64 目标开发环境中使用这个存档(使用 libtransistor )
libtransistor 构建系统使用 LLVM,但它有自己的一组标准包含(libc 等)并且它不使用 glibc。
因此,当我尝试让 libtransistor 链接我的存档时,出现以下错误:
/usr/lib/llvm-5.0/bin/ld.lld: error: undefined symbol: stderr
>>> referenced by gcc_libinit.c:29
>>> 000006.o:(x_cgo_sys_thread_create) in archive ./sdlgotest.a
/usr/lib/llvm-5.0/bin/ld.lld: error: undefined symbol: stderr
>>> referenced by gcc_libinit.c:29
>>> 000006.o:(x_cgo_sys_thread_create) in archive ./sdlgotest.a
顺便说一句,libtransistor 正在使用这样的标志编译代码:
-nostdlib -nostdlibinc -isystem/opt/libtransistor/include/
所以我猜问题是链接器无法解析那些 glibc 符号。
有没有一种方法可以在没有这些 glibc 符号的情况下编译 Go 运行时,这样我就可以像我打算的那样使用存档? (不依赖 GCC 工具链或 glibc)
最佳答案
来自 the docs :
The default C and C++ compilers may be changed by the
CC
andCXX
environment variables, respectively; those environment variables may include command line options.
所以我会尝试类似的东西
$ CC=clang go build -buildmode=c-archive -o test.a main.go
看看会发生什么。
关于使用 libc 而不是 glibc 的 Golang c-archive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53934241/