我想在Windoes和Linux上編譯並運行C++代碼的一個副本。所以我需要使用條件編譯(CC)。然而,它遇到了這個問題,如何使用CC在大量的代碼文件。有大量代碼文件的C++條件編譯
比如,我有:
//A.cpp
#define _WINDOWS_
#ifdef _WINDOWS_
#include <windows.h>
//some code for windows
#else
#include <pthread.h>
//some code for linux
#endif
,我也有B.cpp,C.cpp ...
我應該寫 「的#define _WINDOWS_」 在每一個文件,或它有更好的方法嗎?
,我已經嘗試這樣做: 1.創建一個頭文件Platform.h
#ifndef _PLAT_
#define _PLAT_
#define _WINDOWS_
#endif
2.包括文件Platform.h,如:
//A.cpp
#include "Platform.h"
#ifdef _WINDOWS_
#include <windows.h>
//some code for windows
#else
#include <pthread.h>
//some code for linux
#endif
3它編譯錯誤!在包含導致錯誤之前可能包括「Platform.h」。
謝謝。
你最好使用'_WIN32'而不是滾動你自己的。 – chris
我覺得你需要抽象工廠把你的代碼分離到不同的平臺上嗎? http://en.wikipedia.org/wiki/Abstract_factory_pattern – billz
什麼是編譯錯誤?看起來像它應該至少工作 –