1

I'm working on a multi-platform project. I want a simple and quick way of managing OS-specific includes, preferably without any external tools.

I would like something like this (which I'm well aware won't work)

#define PLATFORM_A 1
#define PLATFORM_B 2

#ifndef TARGET_PLATFORM
//ERROR
#endif

#if TARGET_PLATFORM == PLATFORM_A
    #define DIR "a"
#elif TARGET_PLATFORM == PLATFORM_B
    #define DIR "b"

#define PLATFORM_INCLUDE(file) \
    #include "platforms/" DIR "/" file
4

4 回答 4

5

One set up I see fairly frequently is as follows:

// platform.h

#if defined PLATFORM_A
    #include "platform_a.h"
#elif defined PLATFORM_B
    #include "platform_b.h"
#else
    // #pragma error or whatever
#endif

Then #include "platform.h" where necessary.

于 2013-05-14T19:25:10.617 回答
3

If you have same file tree for different platfroms you could specify only file name for include file in the source code and use platform dependent include directory. This directory could be easily configured by environment variable.

于 2013-05-14T19:24:08.987 回答
2

You can have preprocessor directives defined by the platform itself. In this way you can use:

#ifndef MAC // MAC performs #define MAC

#ifndef WINDOWS // Windows performs #define WINDOWS

#ifndef UNIX // Unix performs #define UNIX

// ERROR

#endif

#endif

#endif
于 2013-05-14T19:21:05.900 回答
-1

Try using + between your strings. That should be the C++ syntax.

So something like:

#define PLATFORM_INCLUDE(file) \
    #include "platforms/" + DIR + "/" file
于 2013-05-14T19:34:09.157 回答