There is nothing inherently "bad" about using the preprocessor. It was created for a reason, and conditional compilation/inclusion like this is one of the things that does rather well.
The key is to keep your code readable. If you end up having a lot of #ifdef WIN32
blocks in your code, or if you find yourself doing this in a large number of files, here are a few tips to clean up your code a bit.
1) Move your conditional inclusion into a separate file. Your source files will simply #include
, and move your conditional inclusion block into defs.h (even if that's all that is in that file).
2) Let your makefile determine which file to include. Again, your source files will simply #include
. Your makefile will detect the current platform and add -iwin32
or -ilinux
to the compiler options string as appropriate. This is the approach that I typically take. You can do the platform detection work inside the makefile or during the configuration stage (if you are dynamically-generating your makefiles). I also find this option easiest in terms of adding support for a new platform at a later date; it seems to minimize the number of changes that need to be made.
3) If you are building on a Linux system (implying that any Win32 code will be generated by a cross-compiler), you can have your build scripts create a symbolic link to the appropriate header file directory. Your code can reference the directory as #include
and let the makefile or configure script worry about symlinking the correct folder.