QT在跨平台中使用很常見,既要開發基於ubuntu,又要開發基於windows情況下,可以使用條件編譯來區分平台.
步驟如下:
1. 在pro文件中添加 DEFINES
eg:
# use linux or windows
DEFINES += RUN_ON_LINUX
2. 在pro文件中區分需要添加的頭文件和源文件
if(contains(DEFINES, RUN_ON_LINUX)){ // 必須寫在同一行
SOURCES += posix_qextserialport.cpp
HEADERS += posix_qextserialport.h
}else{
SOURCES += win_qextserialport.cpp
HEADERS += win_qextserialport.h
}
3.在源文件和頭文件中添加條件選項
#ifdef RUN_ON_LINUX
#include "posix_qextserialport.h"
#else
#include "win_qextserialport.h"
#endif
這樣即可.
