including header file based on OS.

Apr 13, 2009 at 9:24am
Hi Everyone

I have 2 header files; say linuxstdio.h and windowsstdio.h in my project source.

/project/src/linux/include/linuxstdio.h
/project/src/windows/include/windowsstdio.h

I want to include one of these in my build depending on the target i am building it for (linux or windows).

i want to do this in the following way..

 
#include <project/src/__OS__/include/__OS__stdio.h 


Is there a way i can set the __OS__ variable in my build environment and have my C++ compiler pick it up from there?

This is just a simple example of what i want to do. To be specific; i have N such headers files; so i must follow this simple approach; unless there is smthing more simpler..
Last edited on Apr 13, 2009 at 9:32am
Apr 13, 2009 at 9:32am
1
2
3
4
5
6
7
#ifdef WIN32
/project/src/windows/include/windowsstdio.h
#endif

#ifdef __linux__
/project/src/linux/include/linuxstdio.h
#endif 
Apr 13, 2009 at 9:37am
Oh i just realised i asked the wrong query.. apologies..

actually i want to include BOTH the headers.. and i want to do it this way..

 
#include <project/src/__OS__/include/__OS__stdio.h> 


Is there a way i can set the __OS__ variable in my build environment and have my C++ compiler pick it up from there?

This is just a simple example of what i want to do. To be specific; i have N such header files; so i must follow this simple approach; unless there is smthing more simpler..
Last edited on Apr 13, 2009 at 9:38am
Apr 13, 2009 at 10:13am
you can define a variable and then you can compile.
i give you an example of gcc

do this:
export __OS__=<your value>

compile the code with a -D __OS__
like:
g++ -D __OS__ file_name
NOTE: its -D if i remember it correctly.



you code will take the value of __OS__ now, what ever it will be

if you do this:
export __OS__=MyVal1

#ifdef MyVal1

//this will compile if __OS__ value is MyVal1
#endif

#ifdef MyVal2

//this will only compile if __OS__ value is Myval2
#endif


i dont know about ms vc, how to do that in that.
hope this is what you want.
Topic archived. No new replies allowed.