detecting compiler subsystem

Started by
6 comments, last by jchmack 17 years, 1 month ago
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?
Advertisement
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.
[size="1"]
Quote:Original post by mrbastard
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.


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:
properties//linker//system//subsystem
and switchign between windows/console

could you please elaborate for a preprocessor newbie like myself. Maybe with some example code?
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.
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.
[size="1"]
Quote:Original post by ToohrVyk
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.


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.
Quote:Original post by jchmack
well 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.
[size="1"]
yeah my initial fear was that it would take up alot of space because of media files but i can just generate the .exe in the same folder.

This topic is closed to new replies.

Advertisement