如何用Visual Studio编译器通过cgo构建库?相关问题咨询
Answers to Your Cgo & Visual Studio Compiler Questions
1. What are the names of Visual Studio's C and C++ compilers?
- Both the C and C++ compilers included with Visual Studio are
cl.exe. - The compiler automatically differentiates between C and C++ code based on file extensions:
.cfiles are processed as C source code, while.cpp,.cxx, or.ccfiles are treated as C++. - A quick note: You'll need to run commands using
cl.exefrom a Visual Studio Developer Command Prompt (or a terminal where you've initialized the VS build environment viavcvarsall.bat). Otherwise, your system won't locatecl.exeeven if you set the environment variables correctly.
2. Why do you get the "cannot find package" error when setting the compiler?
The error stems from incorrect command syntax. When passing environment variables to go build, you need to define them before the go build command itself—not as positional arguments to it.
Your original (incorrect) command:
go build CC=cl cxx=cl --buildmode=c-shared -o ../lib/core.dll
In this case, Go interprets CC=cl as a package name it's supposed to build, which leads directly to the "cannot find package 'CC=cl'" error.
Correct command variations:
- Unix-like shells (or Git Bash on Windows):
CC=cl CXX=cl go build --buildmode=c-shared -o ../lib/core.dll - Windows Command Prompt (cmd):
set CC=cl && set CXX=cl && go build --buildmode=c-shared -o ../lib/core.dll - Windows PowerShell:
$env:CC='cl'; $env:CXX='cl'; go build --buildmode=c-shared -o ../lib/core.dll
By setting the environment variables first, Cgo will correctly recognize cl.exe as your compiler, instead of trying to treat the variable definitions as package targets.
内容的提问来源于stack exchange,提问作者Liron Levy
相关产品推荐
相关产品推荐

