简体   繁体   中英

How to enable console window in Debug mode and disable in Release mode with QT & CMake?

The develop environment is QT + Windows + C++. How to switch console in different build configuration? Could config expression be used in add_executable to solve it? For example add_executable(main $<$<CONFIG:Debug>:WIN32> $<$<CONFIG:Release>:> main.cpp)

Below is what I use to achieve what you are looking for:

add_executable(<EXE_NAME> ...// sources) # note no WIN32
set_target_properties(
    <EXE_NAME>
    PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE"
               LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup"
)

This will show a console in debug but not in release.

if(WIN32)
    set_target_properties(${PROJECT_NAME}
    PROPERTIES
        LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE"
        LINK_FLAGS_RELEASE "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"
        LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"
        LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"
    )
endif(WIN32)

This work perfect, thanks

Here is an example that should work on Windows & Linux and uses the more specific target_link_options

if (WIN32)
    # if config is debug set the variable to "/SUBSYSTEM:CONSOLE," else "/SUBSYSTEM:WINDOWS", append "/ENTRY:mainCRTStartup>" in any case 
    set(SUBSYSTEM_LINKER_OPTIONS "$<IF:$<CONFIG:Debug>,/SUBSYSTEM:CONSOLE,/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup>")
else()
    # if config is not debug set the variable to "-mwindows" else nothing
    set(SUBSYSTEM_LINKER_OPTIONS "$<IF:$<NOT:$<CONFIG:Debug>>,-mwindows,>")
endif()

target_link_options(TargetName PRIVATE ${SUBSYSTEM_LINKER_OPTIONS})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM