gpt4 book ai didi

c++ - 使用 CLion/CMake/MSVC 2015 编译 WIN32 应用程序时出现链接错误

转载 作者:太空狗 更新时间:2023-10-29 22:56:09 28 4
gpt4 key购买 nike

我无法使用 CLion 和 Visual Studio 2015 作为工具链来编译简单的 WIN32 应用程序。这是我收到的链接器错误:

[ 50%] Linking CXX executable test-test.exe
MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
test-test.exe : fatal error LNK1120: 1 unresolved externals
LINK Pass 1 failed. with 1120

以下是重现该问题的最简化代码版本。它包含在这两个文件中:

  • CMakeList.txt

    cmake_minimum_required(VERSION 3.9.6)
    add_executable(${PROJECT_NAME} WIN32 main.cpp)
  • main.cpp

    #include <iostream>

    int main(int argc, char **argv) {
    std::cout << "HELLO WORLD!" << std::endl;
    return 0;
    }

有关我正在使用的工具的更多详细信息:

  • CLION 2018.1 EAP(最新版)
    • 我还测试了以前的非 EAP 版本,结果相同
    • CLion 的工具链架构设置为 x86_amd64
    • CLion 的 CMake 构建类型设置为调试
  • CMake 3.9.6
  • Visual Studio 2015(社区)

请指教

编辑 1

正如我所提到的,我尽可能地简化了代码以重现错误。因此,使用 std::out。真正的应用程序实际上是QT应用程序,而不是控制台应用程序。

我在 CMakeList.txt 文件中添加了 WIN32 参数,以便应用程序在执行 EXE 时直接启动 GUI(而不是加载命令行提示符,然后加载 GUI)。

最佳答案

错误是意料之中的。它只是遵循该行记录的行为

add_executable(${PROJECT_NAME} WIN32 main.cpp)

来自 add_executable :

If WIN32 is given the property WIN32_EXECUTABLE will be set on the target created. See documentation of that target property for details.

WIN32_EXECUTABLE 的文档:

[...] When this property is set to true the executable when linked on Windows will be created with a WinMain() entry point instead of just main(). [...]

你有两个选择:

  • 删除 WIN32来自 add_executable命令。
  • 替换int main(int, char**)WinMain入口点:int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int) .

如果您不希望弹出控制台窗口,则第二个选项可能是您正在寻找的。

关于c++ - 使用 CLion/CMake/MSVC 2015 编译 WIN32 应用程序时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48915216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com