Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

windows

cmake

gtk

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 which I don't want it there.

How do I get rid of that console?

I tried this:

add_executable(Cmd WIN32 cmd.c)

But got this fatal error:

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
Cmd.exe : fatal error LNK1120: 1 unresolved externals

While using gcc directly works:

gcc -o Cmd cmd.c -mwindows ..

I'm guessing it has something to do with the entry function: int main( int argc, char *argv[]),but why gcc works?

How can I make it work with cmake?

UPDATE

Let me paste the source code here for convenience:

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(window);

  gtk_main();

  return 0;
}

UPDATE2

Why gcc -mwindows works but add_executable(Cmd WIN32 cmd.c) not?

Maybe that's not the equivalent for -mwindows in cmake?

like image 993
Gtker Avatar asked May 02 '10 07:05

Gtker


4 Answers

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.

like image 112
Bill Hoffman Avatar answered Sep 29 '22 16:09

Bill Hoffman


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.

like image 21
Matt Eckert Avatar answered Sep 29 '22 17:09

Matt Eckert


For CMake 3.13 and newer you can use

target_link_options(target PRIVATE "/SUBSYSTEM:WINDOWS" "/ENTRY:mainCRTStartup")
like image 25
eyelash Avatar answered Sep 29 '22 15:09

eyelash


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.

like image 25
Blindy Avatar answered Sep 29 '22 17:09

Blindy