一个可以与操作系统交互的程序,不论其运行在哪个操作系统上。
大多数c/c++编译器都具有定义宏来检测操作系统的能力。
一些GCC编译器的宏包括:
_WIN32:32位和64位Windows操作系统的宏。
_WIN64:64位Windows操作系统的宏。
_UNIX:UNIX操作系统的宏。
_APPLE_:macOS的宏。
基于这些定义的宏,让我们创建一个不受操作系统限制的程序:
示例
实时演示
'#include <iostream>
using namespace std;
int main() {
#ifdef _WIN32
system("dir");
#else
system("ls");
#endif
return 0;
}
输出
'This lists all files of the directory to the output screen irrespective of OS.