c++ - 在 Visual Studio 2005 中编译 Win32 "hello world"的命令行

标签 c++ windows winapi visual-studio-2005

下面的基本 Win32 程序在 Dev-C++ 中编译得很好。

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    MessageBox(NULL,"Hello, world!","My app", MB_OK ) ;
}

但现在我正在尝试使用 Visual Studio 2005 对其进行编译。我打开 Visual Studio 命令提示符并键入:

cl test.cpp

但是我得到以下错误:

test.cpp
test.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function _WinMain@16
test.exe : fatal error LNK1120: 1 unresolved externals

我认为问题可能出在链接器的路径上,但根据这个 MSDN page ,链接器在环境变量 LIB 中查找它,该变量已在 Visual Studio 提示中设置为:

C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB;
C:\Program Files\Microsoft Visual Studio 8\VC\LIB;
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\lib;
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;

在命令行中编译 Win32 程序还需要什么?

我正在为 Vista 使用 Visual Studio 2005 SP1 更新。

最佳答案

user32.lib 添加到命令中:它是user32.dll 的导入库,g++ 默认链接,但Visual C++ 不链接。

一般来说,只要检查链接器反对的任何函数的文档即可。


请注意,您不需要使用那个非标准的 Microsoft 怪物 WinMain

而是使用标准的 C++ main

然后使用 Microsoft 的链接器,如果您想要 GUI 子系统可执行文件,请添加选项 /entry:mainCRTStartup


最小的 C++03 示例:

#define UNICODE
#include <windows.h>

int main()
{
    MessageBox( 0, L"Hello, world!", L"My app:", MB_SETFOREGROUND );
}

使用 Visual C++ 12.0 作为 GUI 子系统可执行文件从命令行构建:

[D:\dev\test]
> set cl & set link
CL=/EHsc /GR /FI"iso646.h" /Zc:strictStrings /we4627 /we4927 /wd4351 /W4 /D"_CRT_SECURE_NO_WARNINGS" /nologo
LINK=/entry:mainCRTStartup /nologo

[D:\dev\test]
> cl winhello.cpp /Fe"hello" /link /subsystem:windows user32.lib
winhello.cpp

关于c++ - 在 Visual Studio 2005 中编译 Win32 "hello world"的命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778319/

相关文章:

c++ - RegOpenKeyEx() 错误

C++20 范围 : reuse of a filter view

c++ - Material 变化回调 (C++)

windows - 在带有 MinGW 的 Windows x64 下使用 GNU 科学库 (GSL)

windows - 适用于 Windows 的视频捕获 SDK 和框架

c++ - 替换 dll 驱动程序及其完整性

c++ - 如何检查单字符串输入的部分是 int 还是 char?

c# - 将 C 结构与 union 编码到 C# 代码时获取垃圾数据

python - 如何为我的 test.py 制作 Windows 独立安装程序

c# - 如何复制具有深层路径的只读文件并保留文件时间元数据?