在windows下编译go语言编写的dll库

go/cgo 用gcc 编译器(虽然Go 在 Windows 上支持用 MSVC 的 cl.exe 作为 cgo 编译器 ,但真正编译时,会有一个问题,下面有详细的说明)

一、下载并安装 MSYS2(Windows)

https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib/msys2-x86_64-latest.exe

  • 默认安装路径建议用:C:\msys64
  • 勾选 "Run MSYS2 now / 运行 MSYS2" 之类的选项(如果有)
  1. 在 MSYS2 UCRT64 里安装 gcc(你现在就在这个窗口)
bash 复制代码
pacman -Syu
pacman -S --needed mingw-w64-ucrt-x86_64-gcc
gcc --version
  1. 在 Windows PowerShell 里让 go/cgo 用 gcc(关键)
    回到 PowerShell:
  • A. 先把 UCRT64 的 bin 加到 PATH(临时)
bash 复制代码
$env:Path = "C:\msys64\ucrt64\bin;" + $env:Path
where.exe gcc
gcc --version
  • B. 配置 Go 使用 gcc
    安装JetBrains 的 goland 工具,把go.exe 安装在了 默认路径下,先在PowerShell设置go的路径
bash 复制代码
$env:Path = "C:\Users\Administrator\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.24.2.windows-amd64\bin;" + $env:Path
go env -w CGO_ENABLED=1
go env -w CC=gcc
go env -w CXX=g++
go env CC CXX CGO_ENABLED
  1. 编译 DLL
bash 复制代码
cd D:\work\go\2026\upfast-go\capi
go build -buildmode=c-shared -o uploader.dll .

运行MSYS2 UCRT64 后,提示 : /usr/bin/cp: cannot stat 'C:\Windows\system32\drivers\etc\hosts': No such file or directory

说明没有找到hosts文件

补上一个文件

右键 PowerShell → "以管理员身份运行"

bash 复制代码
New-Item -ItemType Directory -Force -Path C:\Windows\System32\drivers\etc | Out-Null

@"
# Copyright (c) Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
"@ | Set-Content -Encoding ASCII -Path C:\Windows\System32\drivers\etc\hosts

Go 在 Windows 上支持用 MSVC 的 cl.exe 作为 cgo 编译器

只要确保在"开发者命令行环境"里构建即可

开始菜单 → Visual Studio 2022 文件夹里通常有:

Developer Command Prompt for VS 2022

x64 Native Tools Command Prompt for VS 2022

Developer PowerShell for VS 2022

x64 Native Tools Developer PowerShell for VS 2022

打开其中一个 "x64 Native Tools ..." 就行

env:Path = "C:\\Users\\Administrator\\go\\pkg\\mod\\golang.org\\toolchain@v0.0.1-go1.24.2.windows-amd64\\bin;" + env:Path

在用 MSVC 的 cl.exe 编译时,会报出一个错误 error D8021 :��Ч����ֵ������/Werror

然后用下面的命令 重新编译,就能打印出错误的来源

go build -x -work -buildmode=c-shared -o uploader.dll .

是 Go 在编 runtime/cgo 时自己加了一串 GCC/Clang 风格参数:

从 -x -work 输出可以看到(关键片段):

bash 复制代码
... cgo.exe ... -- ... -O2 -g -Wall -Werror -fno-stack-protector -Wdeclaration-after-statement ...

这套 Go toolchain(用的是 module toolchain go1.24.2)在 Windows 下的 cgo 流程默认按"gcc/clang"来喂参数,所以用 cl 会炸。

要编 -buildmode=c-shared,cgo 必须启用,而 runtime/cgo 这关必过,因此要换成 能吃 -Wall -Werror 的 C 编译器:MinGW-w64 的 gcc 或 clang。

相关推荐
Persistent的粽子!8 分钟前
C++:类与对象(二)
开发语言·c++
Dreams°12313 分钟前
【Java后端+Vue前后端分离:内网正常、公网访问异常|5个高频经典踩坑完整复盘】
java·开发语言·vue.js
.YM.Z1 小时前
C++ STL 容器深度剖析:vector 与 list 的模拟实现及对比
开发语言·c++·vector·list
李燚1 小时前
把规则搬回家:三个 BC 的贫血→充血重构实录(第103篇)
golang·agent·ddd·领域驱动设计·eino·deepflux·eino adk
lsylalalala1 小时前
多线程(4)
java·开发语言·多线程
小白不想画工图1 小时前
银河麒麟V10系统安装QT5.12
linux·开发语言·qt·银河麒麟
IT_Octopus2 小时前
JSON 日志里的 `{“$ref“:“$.xxx“}`:从 fastjson 兼容包到原生 fastjson2 的迁移实录
开发语言·python·json
geovindu2 小时前
python:HandWriting Recognition using paddlepaddle
开发语言·后端·python·paddlepaddle
学海浪太大2 小时前
笔记本电脑插入显示器没反应
windows
wuyk5553 小时前
Python 零基础入门第九章:用户输入与 While 循环
开发语言·python