
一个可以与操作系统交互的程序,不论其运行在哪个操作系统上。
大多数c/c++编译器都具有定义宏来检测操作系统的能力。
一些GCC编译器的宏包括:
_WIN32:32位和64位Windows操作系统的宏。
立即学习“C++免费学习笔记(深入)”;
_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.
以上就是在C/C++中编写与操作系统无关的代码的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号