Go 编译为 Windows 平台下的动态链接库 (DLL),可供 C 程序调用(Linux下为so)

命令

go build -ldflags "-s -w" -buildmode=c-shared -o app.dll .\app.go

解释

  1. 命令组成
    go build: Go 语言编译命令
    -ldflags "-s -w": 链接器标志
    -s: 去除符号表
    -w: 去除调试信息
    -buildmode=c-shared: 构建模式,生成 C 共享库
    -o app.dll: 指定输出文件名为 app.dll
    .\app.go: 指定源文件

  2. 构建目标
    该命令用于将 app.go 编译为 Windows 平台下的动态链接库 (DLL),可供 C 程序调用。

  3. 注意事项
    使用 c-shared 模式需要代码中有 import "C" 和相应的导出函数
    当前代码存在 //export PrintLog 指令,但函数参数类型不符合 CGO 要求
    需要修复导出函数的参数类型问题才能成功构建

例子

image

posted @ 2025-10-23 17:07  一个小笨蛋  阅读(3)  评论(0)    收藏  举报