views:

51

answers:

2

Hi

In my implementation I have a preprocessor definition that specifies the operating system the application is running on, e.g. OS_WIN or OS_LINUX.

In a header file, I have defined the interface, that is the same for each operating system.

//interface.h:

void functionA();
void functionB();

I also have the implementations for the interface for each operating system, e.g.

//windows_interface.c:

#include "interface.h"
void functionA(){
//do something in windows
}
void functionB(){
//do something in windows
}

//linux_interface.c:

#include "interface.h"
void functionA(){
//do something in linux 
}
void functionB(){
//do something in linux
}

And now finally the question ;). How can I use now windows_interface implementation when OS_WIN preprocessor is set and how can I use linux_interface.c when OS_LINUX is defined as preprocessor command?

Thanks

+3  A: 

Don't compile the per-platform C files directly. Instead, make a file, interface.c, which contains

#if OS_WIN

#include "windows_interface.c"

#elif OS_LINUX

#include "linux_interface.c"

#else

#error "Need to implement an interface for this platform!"

#endif

and compile that.

Alternatively - and better still - don't select which set of C code to compile from inside the C code itself - do it in the script or makefile that's controlling the build.

moonshadow
That sounds good. many thanks!
A: 

In one file your surround the whole code with:

#ifdef OS_WIN
…
#endif

and in the other:

#ifdef OS_LINUX
…
#endif

And then you compile both files. That would be the simplest solution, I think.

Michał Górny