在C/C++中编写与操作系统无关的代码
一个能够与操作系统交互的程序,无论它运行在哪个操作系统上。
大多数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.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP