What's the equivalent of gcc's -mwindows option in cmake?

后端 未结 5 1948
后悔当初
后悔当初 2020-12-11 16:42

I\'m following the tuto:

http://zetcode.com/tutorials/gtktutorial/firstprograms/

It works but each time I double click on the executable,there is a console w

相关标签:
5条回答
  • 2020-12-11 17:00

    If you want your program to run in console mode (ie a main function), you have to specify it in your project's properties in MSVC. What you're using right now is a project in windowed mode (ie a WinMain function, which you don't have, hence the error).

    But if you don't want to get the ugly console window, you want to use the windowed mode (ie transform your main function into a propper WinMain function). This way your normal window is all that will show.

    edit: As an aside, you really shouldn't name your program "cmd", that's the name of Windows' command interpreter.

    0 讨论(0)
  • 2020-12-11 17:18

    You can set these linker flags to have a main() entry point and no console:

    SET(CMAKE_EXE_LINKER_FLAGS 
        "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows /ENTRY:mainCRTStartup")
    

    For more info, see this answer for the linker flags, and this answer for how to set flags in cmake.

    0 讨论(0)
  • 2020-12-11 17:19

    For CMake 3.13 and newer you can use

    target_link_options(target PRIVATE "/SUBSYSTEM:WINDOWS" "/ENTRY:mainCRTStartup")
    
    0 讨论(0)
  • 2020-12-11 17:20

    add_executable(Cmd WIN32 cmd.c)

    Tells CMake this is a Windows program, and it looks for WinMain instead of main. If you want to see the flags being used you can run make VERBOSE=1. The question might be how do you define WinMain for gtk apps? I know with Qt, you link in a library that defines it for you.

    0 讨论(0)
  • 2020-12-11 17:24

    According to the CMake documentation for using the WIN32 flag with ADD_EXECUTABLE:

    When this property is set to true the executable when linked on Windows will be created with a WinMain() entry point instead of of just main().This makes it a GUI executable instead of a console application. See the CMAKE_MFC_FLAG variable documentation to configure use of MFC for WinMain executables.

    However, your program's entry point is main() and not WinMain(). What you should do, instead, is omit the WIN32 flag, but you need to link against libgtk. So, you would use TARGET_LINK_LIBRARIES:

    FIND_PACKAGE(GTK2 2.6 REQUIRED gtk)
    INCLUDE_DIRECTORIES(${GTK2_INCLUDE_DIRS})
    LINK_DIRECTORIES(${GTK2_LIBRARIES})
    ADD_EXECUTABLE(myprogramname source1 source2 ... sourceN)
    TARGET_LINK_LIBRARIES(myprogramname ${GTK2_LIBRARIES})
    
    0 讨论(0)
提交回复
热议问题