duanmei1930 2017-10-28 15:58
浏览 154

无法使用通过“ Go build”从Go代码编译的C库

I am trying to get a minimal example of using Go code in C to work, such as this. I am struggling with the following compilation error message:

ld: warning: ignoring file /Users/username/gocode/src/example/example.dylib, file was built for archive which is not the architecture being linked (x86_64): /Users/username/gocode/src/example/example.dylib

My attempt is the following. I have a simple Go package in a file main.go:

package example

import "C"

//export GoEcho
func GoEcho(s *C.char) string {
    return C.GoString(s)
}

func main() {}

which I then compile using either

go build -buildmode=c-archive -o example.dylib main.go

or

go build -buildmode=c-shared -o example.dylib main.go

or

GOARCH=amd64 go build -buildmode=c-shared -o LibraryLinkExamples.dylib main.go

The operating system is OS X 10.11 and the the Go version is, as go version puts it, go1.9 darwin/amd64.

The C code I'm using is the following:

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

int main(int argc, char const *argv[])
{
    GoString res = GoEcho("test");
    printf("%.*s
", (int)res.n, res.p);
    return 0;
}

I don't understand why I am getting the "which is not the architecture being linked (x86_64)" error message when I am building the library and using it on the same operating system, even the computer? What can explain this?

Update In the official documentation here it says:

amd64 (also known as x86-64)

meaning it should be x86-64 compatible with this setting. That it isn't is possibly a bug in go build?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线