QtCreator - building a Win32 application with cmake [SOLVED]



  • I'm trying to use QtCreator to build windows MFC applications using cmake.

    Currently I have a simple test application which is

    CMakeLists.txt

    project(Win32App)
    
    cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
    
    set(SRC
        main.cpp
    )
    
    set(HDR
    
    )
    
    set(RES)
    
    add_definitions(-DWIN32 -D_WINDOWS -D_UNICODE -DUNICODE)
    
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,5.01")
    
    add_custom_target(${CMAKE_PROJECT}_HEADERS SOURCES ${HDR})
    
    add_executable(${CMAKE_PROJECT_NAME} ${SRC} ${RES})
    
    

    main.cpp

    #include <Windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        return 0;
    }
    

    it compiles okay in command line with cmake .. && cmake --build .
    In QtCreator occurs the following error MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

    To me it seems that the /SUBSYSTEM:WINDOWS,5.01 is being ignored during the QtCreator compilation process.

    Any ideias will be welcome.

    Thank you!



  • I found a solution. In case anyone has the same problem just add the following to you CMakeLists.txt:

    if(WIN32)
      set(GUI_TYPE WIN32)
    elseif(APPLE)
      set(GUI_TYPE MACOSX_BUNDLE)
    endif()
    

    then add

    ${GUI_TYPE}
    

    to your target.

    My CMakeLists.txt is now

    project(Win32App)
    
    cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
    
    if(WIN32)
      set(GUI_TYPE WIN32)
    elseif(APPLE)
      set(GUI_TYPE MACOSX_BUNDLE)
    endif()
    
    set(SRC
        main.cpp
    )
    
    set(HDR)
    
    set(RES)
    
    add_definitions(-DWIN32 -D_WINDOWS -D_UNICODE -DUNICODE)
    
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,5.01")
    
    add_custom_target(${PROJECT_NAME}_HEADERS SOURCES ${HDR})
    
    add_executable(${CMAKE_PROJECT_NAME} ${GUI_TYPE} ${SRC} ${RES})
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.