#ifdef linux 是一個預處理指令,用于檢查當前代碼是否在 Linux 系統上編譯
#include <stdio.h>
int main() {
#ifdef linux
printf("This code is running on a Linux system.\n");
#else
printf("This code is not running on a Linux system.\n");
#endif
return 0;
}
在這個示例中,如果代碼在 Linux 系統上編譯,它將輸出 “This code is running on a Linux system.”,否則輸出 “This code is not running on a Linux system.”。請注意,#ifdef 指令只檢查是否定義了 linux 宏,而不是檢查當前操作系統。為了更準確地檢查操作系統,你可以使用 #if defined(__linux__) 或 #if __linux__。