# detecting compiler subsystem

This topic is 3954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im using VS2005 SP1 and I frequently switch my console on and off and right now i have to go to the project properties and have to change the linker properties and the value of #if in the following code:

#if 1
int main(int argc, char *argv[])
#else
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )

#endif
{

MY MAIN STUFF HERE

return 0;
}


my question is, is there a way i can just have the compiler detect what subsystem im using like: #if 1 -> #if SOMETHING so that i dont have to change this value each time?

##### Share on other sites
yes.

Use #ifdef _CONSOLE, and define _CONSOLE in your project properties.

project properties // C/C++ // Preprocessor // Preprocessor definitions

or don't define it for a windowed app. You can then create different project configurations for console/windowed if you want.

##### Share on other sites
Quote:
 Original post by mrbastardyes.Use #ifdef _CONSOLE, and define _CONSOLE in your project properties.project properties // C/C++ // Preprocessor // Preprocessor definitionsor don't define it for a windowed app. You can then create different project configurations for console/windowed if you want.

i apologize im not to familiar with doing stuff like this. Im not exactly sure what you mean... I tried doing this:

if _CONSOLEint main(int argc, char *argv[])#else#define WIN32_LEAN_AND_MEAN#include "windows.h"INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )

but _CONSOLE always evaluates to true so i can only run console apps.

Im switching my properties by going to:
and switchign between windows/console

could you please elaborate for a preprocessor newbie like myself. Maybe with some example code?

##### Share on other sites
You should usually not change between console and windows applications this way. As mrbastard said, it is preferable to create two configurations, set one configuration to console and define _CONSOLE, then set the other configuration to windows.

Then, change between the configurations.

##### Share on other sites
I think you're after something more like

#ifdef _CONSOLE   int main(int argc, char *argv[])#else#define WIN32_LEAN_AND_MEAN#include "windows.h"   INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )#endif

you don't want to check the value of _CONSOLE, you want to check if it's been defined.

BTW I rely on SDL to wrap the main() signature for me, so I'm not sure if the winapi bit is correct or not, sorry.

##### Share on other sites
Quote:
 Original post by ToohrVykYou should usually not change between console and windows applications this way. As mrbastard said, it is preferable to create two configurations, set one configuration to console and define _CONSOLE, then set the other configuration to windows. Then, change between the configurations.

well i dont really want to have multiple configurations i just want it to generate one .exe. Im not too familiar with making new configurations i just use the default debug/release and mostly not even debug. I would like it to just detect what kind of subsystem i have during compile time some how. But i think ill learn to make a new configuration.

##### Share on other sites
Quote:
 Original post by jchmackwell i dont really want to have multiple configurations i just want it to generate one .exe.

It actually works out easier to have multiple configs. It saves you forgetting to change the subsystem back after testing, and means rather than going through all the property pages to change it you can just select the proper configuration and hit build.

It means you can have your console exe and your windowed exe in different folders so you easily know which is which without rebuilding. If you don't need one of them at a given time, then don't build that configuration.

This is probably more important for large solutions with multiple projects though, so do whatever works for you.