繁体   English   中英

C ++代码和C版本宏

[英]C++ code and C version macros

预计这将是一个非常具体的问题。 这可能是因为我缺乏一些谷歌搜索无法找到的基本知识。 如果更有意义,请随意回答问题的更一般版本。

给定一些C ++代码,我想知道它的具体标准版本是否(以及如何),以及它的C标准版本(如果有的话)是否相关。

我已经验证了这个测试代码

#include <cstdio>
int main(void)
{
    printf("%ld\n", _POSIX_C_SOURCE);
    return 0;
}

使用“g ++ -std = c ++ 98”,“g ++ -std = c ++ 11”,“clang ++ -std = c ++ 98”,“clang ++ -std = c ++”编译时打印“200809” 11" 。

(当我使用任何显式标准版本编译C时,根本没有定义_POSIX_C_SOURCE宏)。

这是为什么? 完全没有意义的是,在_POSIX_C_SOURCE中编译C ++ 98效果为200809(即10年 )。

您可能正在寻找两件事:

  • 如果要检测C ++ 98:宏__cplusplus定义为199711L
  • 如果要检测C ++ 11:宏__cplusplus定义为201103L

如果您想检测编译器版本,该站点有大量有关适用的各种宏的信息: http//sourceforge.net/p/predef/wiki/Compilers/

对于_POSIX_C_SOURCE ,这是C标准库中可用功能的属性。 因此,因为您正在使用新的glibc(至少2.10),所以您可以支持这些功能。

对于不报告这些值的C编译器,您可能需要显式包含<features.h>来访问它们。

嗯,我认为这是因为_POSIX_C_SOURCE与任何C ++标准规范无关,而是与POSIX规范有关:

_POSIX_C_SOURCE
          Defining this macro causes header files to expose definitions
          as follows:

          ·  The value 1 exposes definitions conforming to POSIX.1-1990
             and ISO C (1990).

          ·  The value 2 or greater additionally exposes definitions for
             POSIX.2-1992.

          ·  The value 199309L or greater additionally exposes
             definitions for POSIX.1b (real-time extensions).

          ·  The value 199506L or greater additionally exposes
             definitions for POSIX.1c (threads).

          ·  (Since glibc 2.3.3) The value 200112L or greater exposes
             definitions corresponding to the POSIX.1-2001 base
             specification (excluding the XSI extension).

          ·  (Since glibc 2.10) The value 200809L or greater exposes
             definitions corresponding to the POSIX.1-2008 base
             specification (excluding the XSI extension).

您获得的值是您使用的编译器/库支持的默认值。

_POSIX_C_SOURCE可能是编译器扩展。

它是POSIX规范,而不是C ++规范。

所以有些编译器不支持它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM