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

 xjq520 发布于 2023-02-08 10:25

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

#include 

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页面,链接器在环境变量中查找它,该变量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更新.

1 个回答
  • 添加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
    

    2023-02-08 10:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有