Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have different Headers in different platforms? [duplicate]

Tags:

c++

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?

like image 746
Dzyann Avatar asked Nov 27 '25 18:11

Dzyann


2 Answers

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.

like image 194
Caesar Avatar answered Nov 30 '25 10:11

Caesar


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.

like image 41
Sean Avatar answered Nov 30 '25 09:11

Sean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!