C platform preprocessor definition

I am writing a small C ++ library that I need to create on several different platforms, including iPhone, Windows, Linux, Mac and Symbian S60. I wrote most of the code so that it is platform incompatible, but there are some parts that need to be written based on each platform.

I am currently doing this by including a different header depending on the current platform, but I am having problems with this because I'm not sure which preprocessor definitions are defined for all platforms. For windows, I can usually rely on WIN32 or _WIN32. For Linux, I can rely on _UNIX_ browsing, but I'm less sure about other platforms or their 64-bit variants. Does anyone have a list of the various definitions found on the platforms, or do I need to resort to the configuration file or the gcc parameter?

+6
c ++ c-preprocessor portability
source share
5 answers

I have this sourceforge precompilation in my bookmarks.

+9
source share

Neither the C nor C ++ standards define such characters, so you will become dominated by specific implementations of C or C ++. A list of commonly used characters would be helpful, but unfortunately I don't have one.

0
source share

I donโ€™t think there is a universal platform list defining, judging by the fact that every cross-platform library I have seen has ad-hoc config.h full of these things. But you might consider using portable libraries like libpng, zlib, etc.

Here is using libpng

0
source share

Definitions will be exclusively up to your compiler provider. If you use the same compiler (say gcc) on all your platforms, it will be a little easier for you.

You can also try to organize your project so that most of the .h files are platform independent. Separate your implementation (cpp files) into separate files; one for non-specific material and one for each platform. Particular platforms may include 'private' headers, which make sense only for that platform. You may have to make adapter functions in order to get something like this in order to work 100% (when system libraries take slightly different arguments), but I found that it is really useful in the end, and creating a new platform is much easier the future.

0
source share

If you want to see the default preprocessor symbols for a given system on which you have GCC (for example, Mac OS X, iOS, Linux), you can get the full list from the command line, like this:

echo 'main(){}' | cpp -dM 

However, they often have limited use, since at the compilation stage, on which the preprocessor works, most of the characters identify the operating system and CPU type only of the compilerโ€™s system hosting , and not to the target system (for example, when cross-compiling for iOS). On Mac OS X and iOS, the correct way to determine the compilation time characteristics of the target system is

 #include <TargetConditionals.h> 

This will pick up TargetConditionals.h from the platform and SDK used, and then you can determine (for example) endianness and some other characteristics of some macros. (Browse TargetConditionals.h to find out what information you can get.)

0
source share

All Articles