Example Scenario:
Your platforms are Windows, Unix, BSD and Mac. The headers for the platform specific operations are:
-Windows: windows_api.h
-Unix: unix_api.h
-BSD: bsd_api.h
-Mac: mac_api.h
Here's an example on how to implement conditional inclusion for this case:
#define PLATFORM_WINDOWS 0 #define PLATFORM_UNIX 1 #define PLATFROM_BSD 2 #define PLATFORM_MAC 3 /*Defines the system as UNIX*/ #define SYSTEM PLATFORM_UNIX #if SYSTEM == PLATFORM_WINDOWS /*Uses the Windows platform API*/ #include"windows_api.h" #define Shutdown() WinQuit(); #define ShowMessage(s) WinShowMsg(s) #elif SYSTEM == PLATFORM_UNIX /*Uses the Unix platform API*/ #include"unix_api.h" #define Shutdown() XQuit(); #define ShowMessage(s) XShowMsg(); #elif SYSTEM == PLATFORM_BSD /*Uses the BSD platform API*/ #include"bsd_api.h" #define Shutdown() BsdQuit(); #define ShowMessage(s) BsdShowMsg(); #elif SYSTEM == PLATFROM_MAC /*Uses the MAC platform API*/ #include"mac_api.h" #define Shutdown() MacQuit(); #define ShowMessage(s) MacShowMsg(); #else /*Triggers a compilation error*/ #error Unknown platform #endif int main(void) { /*The functions will use the UNIX API*/ ShowMessage("Hello world!"); ShutDown(); return 0; }
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!