在Darwin上为Linux交叉编译CGO应用程序。

huangapple go评论78阅读模式
英文:

Cross compile CGO app on darwin for linux

问题

我在尝试将在OS X上编译的Go应用交叉编译为linux/amd64平台时遇到了问题。该应用程序使用libvips,通过这个vips go包来使用。因此,它使用CGO并且需要使用CGO支持进行编译。

我使用Go 1.4,并运行以下构建命令:

GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build

但是最终出现了链接器错误:

clang: error: linker command failed with exit code 1 (use -v to see invocation)```

我猜想我可能需要添加某种`-ldflags`参数,但不确定。

是否可以以这种方式交叉编译CGO应用程序,或者我需要在目标系统上进行本地构建以避免问题和麻烦?

<details>
<summary>英文:</summary>

I&#39;m having issues trying to cross-compile a Go app on OS X to run on linux/amd64.  The app in question is using [libvips](http://www.vips.ecs.soton.ac.uk/index.php?title=VIPS) via this [vips go package](https://github.com/DAddYE/vips).  As such, it is using CGO and needs to be compiled with CGO support.

I&#39;m on Go 1.4 and running the following build command

`GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build`

but end up with a linker error

```ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)```

I&#39;m assuming I probably need to add some kind of `-ldflags` argument but am not sure.

Is it possible to cross-compile CGO apps in this manner yet, or do I need to do a native-build on the target system to avoid issues and headaches?

</details>


# 答案1
**得分**: 1

请看一下[gonative](https://github.com/inconshreveable/gonative)。它可以让你交叉编译cgo代码(只要你只使用标准库)。

另一种方法是使用Docker编译Linux二进制文件。

<details>
<summary>英文:</summary>

Have a look at [gonative](https://github.com/inconshreveable/gonative). this allows you to cross-compile cgo code (as long as you are just using the stdlib).

Another approach would be to compile the linux binary using docker.

</details>



# 答案2
**得分**: 0

快进到2022年,使用Docker来进行跨平台编译CGO应用程序是您最好且最干净的选择。构建包含正确跨平台编译器和C库的Docker容器。这是我使用Docker进行Cgo应用程序的交叉编译的方法。这是包含Dockerfile的存储库。

<details>
<summary>英文:</summary>

Fast-forwarding to 2022, using docker for cross-compiling CGO apps to other platforms is your best and cleanest choice. Build docker containers that contain the correct cross-platform compiler and C libraries. Here is how I [cross-compile my Cgo applications with Docker](https://dh1tw.de/2019/12/cross-compiling-golang-cgo-projects/). Here is the [repo with the dockerfiles](https://github.com/dh1tw/remoteAudio-xcompile).


</details>



huangapple
  • 本文由 发表于 2015年2月23日 08:46:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/28665377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定