在C或C++代码中,要检查是否在Linux系统上编译,可以使用预处理器指令#ifdef
#includeint 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系统上编译时,它会被自动定义。