C++ preprocessor directive to identify OSX

I'm writing a cross platform console application. It already runs on Windows, Linux (all distros), Unix, and OSX. There is one system call in the code to clear the screen. Linux, Unix, and OSX use "clear" to clear the screen, while Windows (at the console level) uses "cls".

While it is a trivial problem to change a macro each time I compile for a different platform, it would be nice to have a preprocessor directive do this for me. Can anyone help out here? What preprocessor directive would I use to identify that I'm compiling on/for OSX?

Thanks.

6 Windoze, 4 Linuxen & 4 OS X (Mac Pro 2.66 Ghz, 1 iBook, MBP 2.0, MB 2.0), Mac OS X (10.4.8), More iPods than people in my house

Posted on Oct 18, 2006 9:06 AM

Reply
3 replies

Oct 18, 2006 2:43 PM in response to Marc Feldesman

It depends. WIN32 is probably going to be defined. MSC_VER is probably going to be defined too. Check for your documentation for your Windows compiler. I am hedging my answer a bit because the answer depends on what compiler you are using. If you are using GCC, then you may not have those symbols defined. But, if you are using GCC on Windows then you probably have this problem to begin with.

A better approach would be to write a "clear()" function for Windows that calls "cls". Then, your application doesn't need any OS identification. You would put the "clear()" function and all other Windows-specific code into a glue.c source file that is only linked into the Windows version.

A good goal to aim for is to make your core application logic independent of any operating system. Each build system for each OS would have a set of ancillary files (like the glue.c source) that enables it to build for that OS. You want to keep that ancillary set of files as small as possible. If you have a console application, that should be easy. Code to the curses library and install some Windows curses port.

Oct 19, 2006 3:29 AM in response to Marc Feldesman

Hi, try this:


cohi@tigger:/tmp> cat t.c
#ifdef _APPLE_
#error darwin detected
#else
#error other detected
#endif

cohi@tigger:/tmp> gcc t.c
t.c:2:2: error: #error darwin detected

cohi@tigger:/tmp> gcc -v
Using built-in specs.
Target: powerpc-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5363.obj~28/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c +,obj-c+ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=powerpc-apple-darwin8 --host=powerpc-apple-darwin8 --target=powerpc-apple-darwin8
Thread model: posix
gcc version 4.0.1 (Apple Computer, Inc. build 5363)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

C++ preprocessor directive to identify OSX

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.