gpt4 book ai didi

C++ 在 Windows 和 Linux 上编译 : ifdef switch

转载 作者:IT老高 更新时间:2023-10-28 11:30:46 24 4
gpt4 key购买 nike

我想在 Linux 和 Windows 上运行一些 c++ 代码。我只想为一个操作系统而不是另一个操作系统包含一些代码。有没有一个标准的#ifdef 曾经可以使用?

类似:

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

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

最佳答案

使用:

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

#endif

关于C++ 在 Windows 和 Linux 上编译 : ifdef switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6649936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com