I am pretty new with C++, and I have a file I have to build both in Linux and Windows.
I was wondering if there was a way I could have different list of #include in the same file for windows and linux, but that somehow I tell it to do so without having to modify the file each time.
Like:
if_Windows
#include <Include1>
if_Linux
#include <Include2>
Is it possible to do something like this? How do you do it?
You could define a macro that you set when you are compiling for windows and Linux.
#define MYPROJECT_CONFIG_WINDOWS_PLATFORM
#if defined(MYPROJECT_CONFIG_WINDOWS_PLATFORM)
#include <include1>
#elif defined(MYPROJECT_CONFIG_LINUX_PLATFORM)
#include <include2>
#else
#error link: no suitable library
#endif
All you then have to do is define MYPROJECT_CONFIG_WINDOWS_PLATFORM when compiling for Window and MYPROJECT_CONFIG_LINUX_PLATFORM when compiling for Linux.
Some IDEs also have pre-set macros for the platform they are compiling for (Like WIN32 for Windows) but you will have to check the documentation for them.
You need to use #ifdef in combination with an appropriate macro for you environment. For example
#ifdef WIN32
// Windows includes
#elseif __unix__
// Unix included
#endif
You'll need to check your compiler documentation to see what they define for the appropriate platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With