我想得到一个 windows-DLL,但我想在 Ubuntu-Linux 下编译它。
构建可执行文件很简单:env GOOS=windows GOARCH=386 go build wrapper.go
生成一个 wrapper.exe
,其行为符合预期。
但是使用 env GOOS=windows GOARCH=386 go build -buildmode=c-shared wrapper.go
构建 DLL 会导致错误:
running gcc failed: exit status 1
gcc: error: unrecognized command line option ‘-mconsole’; did you mean ‘--compile’?
我不想在 windows 下安装和运行 go
,因为我的完整工具链是在 Ubuntu 下运行的
go version go1.15.6 linux/amd64
答案 0 :(得分:6)
如果您将 -x
传递给对 go build -buildmode=c-shared ...
的调用,您会注意到在这种模式下,Go 工具链中的链接器调用外部 C 链接器;例如,在带有 Go 1.15.x 的 GNU/Linux 上,我有:
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-arguments
loadinternal: cannot find runtime/cgo
/home/username/devel/golang-1.15.6/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
gcc: error: unrecognized command line option ‘-mconsole’; did you mean ‘--compile’?
请注意,pkg/tool/linux_amd64/link
是用 -extld=gcc
调用的,从 go doc cmd/link
,我们收集到
-extld 链接器
设置外部链接器(默认为“clang”或“gcc”)。
我的猜测是,为了生成与 C 兼容的动态库,Go 工具链依赖于外部 C 链接器,这是由 cgo
machinery 执行的——实际上在 {{} 中有一个提示3}}:
-buildmode=c-shared
构建列出的主包,以及它导入的所有包,
到 C 共享库。唯一可调用的符号将
是那些使用 cgo
//export
注释导出的函数。
只需要列出一个主要包。
因此我的猜测是,为了做你想做的事,你必须:
-buildmode=c-shared
开始。go build
之前设置环境,以便 Go 工具链调用特定于 Windows 的链接器。go build
命令行选项运行 -x
来验证它是否有效。