在 Linux for Windows 下交叉编译共享库

我想得到一个windows-DLL,但我想在Ubuntu-Linux下编译它。


构建可执行文件很简单:生成一个 ,其行为符合预期。env GOOS=windows GOARCH=386 go build wrapper.gowrapper.exe


但是构建 DLL 会导致错误:env GOOS=windows GOARCH=386 go build -buildmode=c-shared wrapper.go


running gcc failed: exit status 1

gcc: error: unrecognized command line option ‘-mconsole’; did you mean ‘--compile’?

我宁愿不在Windows下安装和运行,因为我的完整工具链在Ubuntu下运行。go


go version go1.15.6 linux/amd64


HUX布斯
浏览 166回答 1
1回答

精慕HU

如果您要传递给 的调用,您会注意到在该模式下,Go 工具链中的链接器会调用外部 C 链接器;例如,在GNU/Linux与Go 1.15.x上,我有:-xgo build -buildmode=c-shared ...mkdir -p $WORK/b001/exe/cd $WORK/b001/exe//home/username/devel/golang-1.15.6/pkg/tool/linux_amd64/link -o cshared.dll -importcfg $WORK/b001/importcfg.link -buildmode=c-shared -buildid=OJVN3iT0GI_DEAMVbLDu/o9eT_YGfUiRe07beNQAA/-xRRfDcM8nVc03rltdqz/OJVN3iT0GI_DEAMVbLDu -extld=gcc $WORK/b001/_pkg_.a# command-line-argumentsloadinternal: cannot find runtime/cgo/home/username/devel/golang-1.15.6/pkg/tool/linux_amd64/link: running gcc failed: exit status 1gcc: error: unrecognized command line option ‘-mconsole’; did you mean ‘--compile’?请注意,与 一起调用,从 go doc cmd/link 中,我们收集pkg/tool/linux_amd64/link-extld=gcc-extld 链接器设置外部链接器(默认为“clang”或“gcc”)。我的猜测是,为了生成一个兼容C的动态库,Go工具链依赖于一个外部的C链接器,这是由cgo机制执行的 - 在-buildmode=c-shared的文档中实际上有一个提示:-buildmode=c-shared将列出的主包及其导入的所有包构建到 C 共享库中。唯一可调用的符号是使用注释导出的那些函数。只需要列出一个主包。cgo//export因此,我的猜测是,为了做你想做的事情,你必须:安装支持 Windows/i386 的交叉编译器 — 你可以从这里开始。按照 cgo 文档中的说明在调用之前设置环境,以便 Go 工具链调用特定于 Windows 的链接器。go build通过使用命令行选项运行来验证它是否正常工作。go build-x
打开App,查看更多内容
随时随地看视频慕课网APP