在C或C++代碼中,要檢查是否在Linux系統上編譯,可以使用預處理器指令#ifdef
#include <iostream>
int main() {
#ifdef __linux__
std::cout << "This code will be compiled on Linux." << std::endl;
#else
std::cout << "This code will not be compiled on Linux." << std::endl;
#endif
return 0;
}
在這個例子中,如果代碼在Linux系統上編譯,它將輸出"This code will be compiled on Linux.“,否則輸出"This code will not be compiled on Linux.”。__linux__
是一個預定義的宏,當在Linux系統上編譯時,它會被自動定義。