help with program selecting OS

Hello.
I am making a program which needs to do specific tasks depending on OS. How can i make a code like this:
1
2
3
4
if OS=windows
{do something}
else if OS=linux
{do something}

Thanks!
Last edited on
Generally, you'll use macros or the like. This can get complicated quickly, so generally you'll use your build system for this. The syntax would be expressed like this:
1
2
3
4
5
6
7
8
9
#ifdef OS_WINDOWS
  // windows specific code
#elifdef OS_MACOSX
  // mac specific code
#elifdef OS_LINUX
  // assume linux
#else
  #error Unrecognised operating system!
#endif 

Then, you simply compile the code based on your operating system, for example on a Debian machine with GCC you might do:
g++ -c -DOS_LINUX -O2 -Wall -std=c++11 file.cpp -o file.o


Compilers also often provide their own predefined macros telling you what operating system you are using, but due to the fact that they aren't standardized they all seem to use different ones, and it can be a pain to keep track of.
Last edited on
Thanks for answer!
Topic archived. No new replies allowed.