使用Golang 1.10无法编译Windows DLL [英] Trouble compiling Windows DLL using Golang 1.10
问题描述
我在使用Golang 1.10构建Windows DLL时遇到问题,最新版本对此版本提供了支持:
I'm having issues building a Windows DLL in Golang 1.10, which is supported in this latest version:
各种构建模式已移植到更多系统上特别是,c-shared现在可以在linux / ppc64le,windows / 386和windows / amd64上运行; (来源: https://golang.org/doc/go1.10 )
"The various build modes have been ported to more systems. Specifically, c-shared now works on linux/ppc64le, windows/386, and windows/amd64;" (Source: https://golang.org/doc/go1.10)
我现在有一个非常简单的程序( main.go
),该程序仅导出一个函数 Test,但是使用以下 go build命令时出现问题: env GOOS = windows GOARCH = 386 go build -buildmode = c-shared main.go
I have a very simple program right now (main.go
) that only exports one function "Test", but am having issues when using the following "go build" command: env GOOS=windows GOARCH=386 go build -buildmode=c-shared main.go
具体地说,收到无法加载软件包:软件包main:构建约束排除[PATH]
错误中的所有Go文件。 main.go
的源代码如下所示:
Specifically, receiving the can't load package: package main: build constraints exclude all Go files in [PATH]
error. The source code for main.go
is shown below:
package main
import (
"C"
"fmt"
)
func main() {
fmt.Println("from main")
}
//export Test
func Test() string {
return "this is a test"
}
我以前从未遇到过此错误,并且在未指定 GOOS $ c的情况下进行构建$ c>和
GOARCH
起作用。希望有人遇到此问题,可以为我提供帮助。
I've never encountered this error before and building without specifying GOOS
and GOARCH
works. Hoping someone has encountered this issue and can help me out.
推荐答案
-
请确保您在Ubuntu上安装了MinGW:
sudo apt-get install gcc-mingw-w64-i686
和sudo apt-get install gcc-mingw-w64-x86 -64
使用以下命令进行编译: GOOS = windows GOARCH = 386 CGO_ENABLED = 1 CC = i686-w64-mingw32-gcc转到构建-buildmode = c-shared -o main.dll main.go
和 GOOS = windows GOARCH = amd64 CGO_ENABLED = 1 CC = x86_64- w64-mingw32-gcc go build -buildmode = c-shared -o main.dll main.go
Compile using the following commands: GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -buildmode=c-shared -o main.dll main.go
and GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -buildmode=c-shared -o main.dll main.go
验证生成的DLL是否有效通过测试测试导出: rundll32.exe main.dll,Test
Verify generated DLL works by testing the "Test" export: rundll32.exe main.dll,Test
这篇关于使用Golang 1.10无法编译Windows DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!