12

One of the things I find nice about CodeBlocks is that it allows me to quickly create, compile and run a file without creating a project. However, this means that all of my programs have console windows, something that isn't normal for most Windows applications.

To get rid of it, I've always had to create a project, throw my file in, and navigate to where I could just click GUI Application instead of Console Application.

However, it's obviously possible to specify the same thing when building using g++.exe and ld.exe, and CodeBlocks has a section for additional linker options, so I figured I could stick it in there to avoid the hassle of always creating a project, but apparently I was wrong.

Firstly, I found this question. I was surprised when I found out who answered it, but that doesn't help me, as the whole point of this is that I can do it with about the same amount of effort without creating a project.

Apparently, the -mwindows compiler option will do that, so I tried putting that in Settings\Compiler and Debugger\Compiler settings\Other options, and it compiles and links fine, but still has a console window.

Next, I tried Settings\Compiler and Debugger\Linker settings\Other linker options. Fuelled by Google results, I've tried adding the following to there, one option active at a time, and rebuilding. Having -mwindows active makes no difference as far as I can tell.

-Wl: Unrecognized command line option
--subsystem,windows: Unrecognized command line option
--subsystem, windows: windows - No such file or directory.
--subsystem windows: windows - No such file or directory.
--subsystem=windows: Unregocnized command line option
--subsystem, console: console - No such file or directory.

This testing was all done on the newest release of CodeBlocks, CodeBlocks 12.11, and GCC 4.7.2, obtained from this MinGW distro (version 9.4). However, I'm fairly certain it works the same way with CodeBlocks 10.05.

Am I forced to use a project, use a makefile, or build it from the command line, or is it possible to change this setting right in the CodeBlocks IDE global settings?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
chris
  • 60,560
  • 13
  • 143
  • 205
  • *"but still has a console window"*. Did the GUI window showed up? – Mark Garcia Dec 19 '12 at 04:04
  • @MarkGarcia, The one I'm testing on actually has no GUI window. However, I've put GUI-less programs into a project and done it that way, which worked fine: Upon starting the program, the user wouldn't notice anything. Before services are mentioned, it needs to interact. It's actually going to be started from a service. Normally, I'd adhere to the common practices, but this one isn't exactly a normal application - it's meant as a joke. The technique explained in an answer, however, can apply just as well to programs *with* a GUI :) – chris Dec 19 '12 at 04:09
  • @MarkGarcia, Sorry, I realize I haven't answered your question in all of that. I found one with a window and tried compiling and running that with `-mwindows`. Both the console and the window show up. – chris Dec 19 '12 at 04:19
  • Hmmm... What version of CodeBlocks? 12.11 is the latest. – Mark Garcia Dec 19 '12 at 04:23
  • @MarkGarcia, I just finished adding that information near the bottom of the question. – chris Dec 19 '12 at 04:23
  • From what I know passing "-Wl,--subsystem,windows" to the linker is the proper way to do this. Are you sure you input these arguments properly? Anyway, I always used MinGW builds from sourceforge. – ZalewaPL Dec 20 '12 at 09:37
  • @ZalewaPL, Wow, that works perfectly! It's actually even better than the project option because it leaves a console whilst running from the IDE, but has no console when running outside of the IDE, whereas the project option has no console either way. I like using `cout` to output a bit of debugging info sometimes, so that will really help. I tried `-mwindows` again without that option, but yes, it still leaves the console when running from explorer. Please make that an answer, as I'm sure many people are dying to hear it. – chris Dec 20 '12 at 14:50
  • While that option was pointed out in the other question, I believe I saw a space in there, and I guess that, combined with the OP saying it didn't work, tricked me into not trying it. – chris Dec 20 '12 at 14:54

1 Answers1

8

Passing -Wl,--subsystem,windows to the linker is the proper way to do this. If it appears not to work then make sure that you didn't insert a space somewhere; use the argument as it is.

ZalewaPL
  • 1,104
  • 1
  • 6
  • 14