How to detect operating system with C preprocessor

2

I would like to do a program but there is a code that works only on Windows and another on GNU / Linux, I would like to know how I can use the preprocessor macros to do this, read something about it, that proposed this example :

int main()
{

    #ifdef WINDOWS
       //Codigo de Windows
    #endif

    #ifdef LINUX
       //Codigo de Linux
    #endif

}

What are the Windows directives?

That is, when doing #ifdef WINDOWS is checking if the macro WINDOWS is defined, and how should that definition be above the function main() ?

Extra: Example code using ifdef

#define PI 3,14

int main()
{
    #ifdef PI

     puts("Casa");
      #enifdef
}
    
asked by EmiliOrtega 15.02.2017 в 23:36
source

1 answer

3

There are certain predefined macros which can be used for the problem you have:

#ifdef __linux__ 
    // Aca incluyes tu codigo para GNU/Linux
#elif _WIN32
    // Aca incluyes tu codigo para MS/Windows
#else

#endif

Update

Code Example:

#include <stdio.h>
#include <stdlib.h>

#ifdef __linux__
#define SO "Linux"
#elif _WIN32
#define SO "Windows"
#endif

int main(void){

    printf("I <3 %s\n", SO);

    return EXIT_SUCCESS;
}
  

Result:

     

I

answered by 16.02.2017 в 00:02