我正在尝试在命令行上使用 Visual Studio 2008 构建程序。 看完Walkthrough: Compiling a Native C++ Program on the Command Line . 我尝试了以下方法:
运行 vcvaralls.bat 来设置环境:
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
编写了这个简单的 C++ 应用程序:
#define _WIN32_WINNT 0x501
#include <windows.h>
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBoxA(0,"Hello","Hello",MB_OK);
return 0;
}`
并尝试编译它:
cl /EHsc /GA simple.cpp
这发生了:
/out:simple.exe
simple.obj
simple.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function _WinMain@16
simple.exe : fatal error LNK1120: 1 unresolved externals
这让我相信我需要包含 user32.lib 或类似文件。 我无法从 visual studio 手册中弄清楚如何做到这一点。
最佳答案
是的,要使用 MessageBox,您至少需要链接到 User32.lib,如图所示 here .使用:
cl /EHsc /GA /MT simple.cpp User32.lib
/MT 选择运行时库。在这个例子中,我使用了多线程静态库。
关于c++ - 在命令行上使用 Visual Studio 进行编译时如何包含库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21928331/