dongxifu5009 2018-12-26 15:56
浏览 366

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

Is it possible?

I have the following Go function I'd like to call from a C program:

// package name: test
package main

import "C"

//export Start
func Start() {
    println("Hello world")
}

func main() {
}

I'm using the following command to build the archive

go build -buildmode=c-archive -o test.a main.go

Using gcc I can get this C program working:

#include <stdio.h>
#include "test.h"

int main() {
  Start();
  return 0;
}

I'm using the following command to build the executable:

gcc main.c sdlgotest.a -o main -lpthread

This all works fine for amd64, but I'd like to use this archive in a aarch64 targed development environment (using libtransistor)

libtransistor build system uses LLVM but it has it's own set of standard includes (libc, etc.) and it doesn't use glibc.

So when I'm trying to get libtransistor to link my archive I get the following errors:

/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 by the way is compiling the code with flags like this:

-nostdlib -nostdlibinc -isystem /opt/libtransistor/include/

So I guess the problem is that the linker can't resolve those glibc symbols.

Is there a way to compile the Go runtime without those glibc symbols so I can use the archive like I intend to? (without relying on GCC toolchain or glibc)

  • 写回答

2条回答 默认 最新

  • dongqie2010 2018-12-26 18:19
    关注

    From 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.

    So I'd try something like

    $ CC=clang go build -buildmode=c-archive -o test.a main.go
    

    and see what happens.

    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?