登录
首页 >  Golang >  Go问答

在mac/windows上启用CGO进行Linux AMD64的交叉编译

来源:stackoverflow

时间:2024-03-04 13:03:25 388浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《在mac/windows上启用CGO进行Linux AMD64的交叉编译》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

我的目标是使用gosseract进行交叉编译的项目,需要有cgo_enable=1。经过多次尝试,我最终编译了 mac 和 windows 原生版本。我的笔记本电脑上的 mac 和使用 msys2 和 mingw 的外国项目 vm 中的 windows。

但我不想要第三台 linux 机器。我测试了一些 docker 解决方案,但它们从未适用于 windows 或 mac。

所以我想使用 mac 或 windows vm(带有 msys2 和 pacman)针对 linux amd64 进行编译。

我的 mac 命令:

env goos=darwin goarch=amd64 cgo_enabled=1 go build -o ../build/darwin/fileinspector

对于 windows:

env goos=windows goarch=amd64 cgo_enabled=1 cc=x86_64-w64-mingw32-gcc cxx=x86_64-w64-mingw32-g++ cgo_cxxflags="-ic:\msys64\mingw64\include" go build -o ../build/windows/fileinspector.exe

对于 mac 上的 linux,有错误:

env goos=linux goarch=amd64 cgo_enabled=1 cgo_cxxflags="-i/usr/local/cellar/leptonica/1.82.0/include -i/usr/local/cellar/tesseract/5.1.0/include" go build -o ../buidl/linux/fileinspector
linux_syscall.c:67:13: error: implicit declaration of function 'setresgid' is invalid in c99 [-werror,-wimplicit-function-declaration]
linux_syscall.c:67:13: note: did you mean 'setregid'?
/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx.sdk/usr/include/unistd.h:593:6: note: 'setregid' declared here
linux_syscall.c:73:13: error: implicit declaration of function 'setresuid' is invalid in c99 [-werror,-wimplicit-function-declaration]
linux_syscall.c:73:13: note: did you mean 'setreuid'?
/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx.sdk/usr/include/unistd.h:595:6: note: 'setreuid' declared here
make: *** [gobuildlinux] error 2

从 windows 到 linux,有错误:

env goos=windows goarch=amd64 cgo_enabled=1 cc=x86_64-w64-mingw32-gcc cxx=x86_64-w64-mingw32-g++ cgo_cxxflags="-ic:\msys64\mingw64\include" go build -o ../build/windows/fileinspector.exe
gcc_linux_amd64.c: In function '_cgo_sys_thread_start':
gcc_linux_amd64.c:61:9: error: unknown type name 'sigset_t'; did you mean '_sigset_t'?
   61 |         sigset_t ign, oset;
      |         ^~~~~~~~
      |         _sigset_t
gcc_linux_amd64.c:66:9: error: implicit declaration of function 'sigfillset' [-Werror=implicit-function-declaration]
   66 |         sigfillset(&ign);
      |         ^~~~~~~~~~
gcc_linux_amd64.c:61:23: error: unused variable 'oset' [-Werror=unused-variable]
   61 |         sigset_t ign, oset;
      |                       ^~~~
cc1.exe: all warnings being treated as errors
make: *** [Makefile:56: gobuildLinux] Error 2

我可能需要不同的编译器,但我不知道。


正确答案


你应该尝试使用 docker/podman 进行跨平台构建,否则你会遇到可怕的问题......

以上就是《在mac/windows上启用CGO进行Linux AMD64的交叉编译》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>