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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)