可以与操作系统进行交互的程序,而与运行该操作系统的操作系统无关。
大多数c / c ++编译器都有能力定义检测OS的宏。
GCC编译器的一些宏是-
_WIN32:32位和64位Windows操作系统的宏。
_WIN64:用于64位Windows操作系统的宏。
_UNIX:UNIX OS的宏。
_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.