在 Windows 和 Linux 上编译 C++:ifdef 开关 [英] C++ compiling on Windows and Linux: ifdef switch

查看:48
本文介绍了在 Windows 和 Linux 上编译 C++:ifdef 开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Linux 和 Windows 上运行一些 C++ 代码.有一些代码片段我只想包含在一个操作系统中,而不包含在另一个操作系统中.是否有一个标准的 #ifdef 曾经可以使用?

I want to run some c++ code on Linux and Windows. There are some pieces of code that I want to include only for one operating system and not the other. Is there a standard #ifdef that once can use?

类似于:

  #ifdef LINUX_KEY_WORD
    ... // linux code goes here.
  #elif WINDOWS_KEY_WORD
    ... // windows code goes here.
  #else 
  #error "OS not supported!"
  #endif

<小时>

这个问题确实是重复的,但这里的答案要好得多,尤其是被接受的答案.


The question is indeed a duplicate but the answers here are much better, especially the accepted one.

推荐答案

use:

#ifdef __linux__ 
    //linux code goes here
#elif _WIN32
    // windows code goes here
#else

#endif

这篇关于在 Windows 和 Linux 上编译 C++:ifdef 开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆