当编译dll文件的时候报如下错误:
PS D:\Programming\go\cmcc-backup-dll> go build -buildmode=c-shared -o helloworld.dll .
package aolingo.com/cmcc-backup-dll: build constraints exclude all Go files in D:\Programming\go\cmcc-backup-dll
PS D:\Programming\go\cmcc-backup-dll> go build -o helloworld.dll -buildmode=c-shared helloworld.go
go: no Go source files
这是因为当编译dll的时候会启动cgo模式,必须在系统中安装c语言的编译环境。
Windows11下安装C编译环境
下载msys2
https://www.msys2.org/
下载并安装后,会自动启动一个命令行终端,执行如下语句,更新gcc
pacman -Syu
pacman -S mingw-w64-x86_64-gcc
如果没有自动打开终端,可以到开始菜单中查找【MSYS2 MINGW64】,并且运行上述命令
建议配置完成后重启一次电脑。
编写一个简单的go项目测试
创建项目
mkdir cmcc-backup-dll && cd cmcc-backup-dll
go mod init aolingo.com/cmcc-backup-dll
编写helloworld.go
package main
/*
#include<stdlib.h>
*/
import "C"
//export HelloWorld
func HelloWorld() *C.char {
message := "wofjwojfwofjwf"
return C.CString(message)
}
func main() {}
编译
PS D:\Programming\go\cmcc-backup-dll> ls
目录: D:\Programming\go\cmcc-backup-dll
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2025/10/10 15:34 46 go.mod
-a---- 2025/10/10 16:02 2573132 helloworld.dll
-a---- 2025/10/10 15:44 187 helloworld.go
-a---- 2025/10/10 16:02 2032 helloworld.h
PS D:\Programming\go\cmcc-backup-dll> $env:CGO_ENABLED=1; go build -buildmode=c-shared -o helloworld.dll .
编译或运行时,由于dll没有签名导致错误的认为病毒的解决办法
System.DllNotFoundException:“Unable to load DLL ‘dll/helloworld.dll’ or one of its dependencies: 无法成功完成操作,因为文件包含病毒或潜在的垃圾软件。 (0x800700E1)”
这个错误是因为 Windows Defender 或您的杀毒软件将编译的 DLL 误报为恶意软件。以下是几种解决方案:
解决方案 1:添加 Windows Defender 排除项
- 打开 Windows 安全中心
- 点击开始菜单 → 设置 → 更新和安全 → Windows 安全中心
- 或者直接搜索”Windows 安全中心”
- 添加排除项
- 点击”病毒和威胁防护”
- 选择”病毒和威胁防护设置”
- 点击”添加或删除排除项”
- 添加以下排除项:
- 您的项目目录:
D:\Programming\go\cmcc-backup-dll\ - Go 编译临时目录:
C:\Users\xxxxx\AppData\Local\Temp\go-build\
- 您的项目目录: