windows - 使用 Golang 1.10 编译 Windows DLL 时遇到问题

标签 windows go dll compilation

我在 Golang 1.10 中构建 Windows DLL 时遇到问题,最新版本支持该版本:

“各种构建模式已经移植到更多系统。具体来说,c-shared 现在可以在 linux/ppc64le、windows/386 和 windows/amd64 上运行;” (来源:https://golang.org/doc/go1.10)

我现在有一个非常简单的程序 (main.go),它只导出一个函数“Test”,但是在使用以下“go build”命令时遇到问题:env GOOS=windows GOARCH=386 go build -buildmode=c-shared main.go

具体来说,收到 can't load package: package main: build constraints exclude all Go files in [PATH] 错误。 main.go 的源代码如下所示:

package main

import (
    "C"
    "fmt"
)

func main() {
    fmt.Println("from main")
}

//export Test
func Test() string {
    return "this is a test"
}

我以前从未遇到过这个错误,并且在没有指定 GOOSGOARCH 的情况下构建也能正常工作。希望有人遇到过这个问题并能帮助我。

最佳答案

  1. 确保您在 Ubuntu 上安装了 MinGW:sudo apt-get install gcc-mingw-w64-i686sudo apt-get install gcc-mingw-w64-x86 -64

  2. 使用以下命令编译:GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -buildmode=c-shared -o main.dll main.goGOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -buildmode=c-shared -o main.dll main.go

  3. 通过测试“测试”导出来验证生成的 DLL 是否正常工作:rundll32.exe main.dll,Test

关于windows - 使用 Golang 1.10 编译 Windows DLL 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49078510/

相关文章:

java - 如何在java中查找在端口号上运行的进程的进程ID

windows - JavaScript - 提取文件夹名称

http - Golang clean items out of channel 不会被读取

c# - 如何卸载动态加载的 DLL

c++ - 如何在运行时检查 void* 是否为 IUnknown*?

c++ - 为cpp创建一个没有命名空间的dll

gomail noauth 示例崩溃

for-loop - "Select"goroutine 内的 for 循环语句

c++ - 编译器是否能够对从 dll 导入的函数进行程序集成?

c# - P/Invoke 是否执行 DLL 然后将其关闭?