Sunday, August 2, 2009

In C, how do you tell what platform your on?

In C, how do you tell whether your on linux, windows, mac, freebsd, etc.?





I know its a preprocessor thing. I just don't know what the constants are...

In C, how do you tell what platform your on?
It depends on which compiler(s) you are using. The standard headers for the given platform will likely define a constant to a suitable value - though the name of that constant, and the value will vary from compiler to compiler.





If you need a particular consistent value, you can easily define your own constant for each platform using the (usually) -D option of the compiler at build time.





edit:





gcc varies by platform and version (see link below) - It's not consistent - you're better off defining your own with the -D option if you need to write platform-dependent code.





You can run your command line compiler on a simple project and observe which options are passed in to see which symbols are pre-defined. Look for the -D option in the compiler command line.


No comments:

Post a Comment