使用 libc 而不是 glibc 的 Golang c-archive

标签 go linker cgo

这可能吗?

我想从 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 and CXX 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/

相关文章:

json - 在 Go 中获取内部 JSON 值

language-agnostic - 为什么静态和动态可链接库不同?

c++ - Visual Studio 2013 Professional 链接器错误

c - 如何与CGO达成共识?

go - 如何执行这个 Go 二进制文件?

Golang 与 couchbase 集成问题

使用dbus头文件编译C程序

windows - 执行 : "gcc": executable file not found in %PATH% when trying go build

linux - golang 使用父进程命名空间在 linux 中读取文件

json - 1 个接口(interface),2 个包,相同的结构变量名称但不同的 json 命名约定