简体   繁体   English

C ++代码和C版本宏

[英]C++ code and C version macros

This is expected to be a too specific question. 预计这将是一个非常具体的问题。 That's probably because I lack some basic knowledge that I can't find by googling. 这可能是因为我缺乏一些谷歌搜索无法找到的基本知识。 Feel free to answer a more general version of the question if that makes more sense. 如果更有意义,请随意回答问题的更一般版本。

Given some C++ code, I would like to know whether (and then how) its specific standards version, and its C standards version (if any) correlate. 给定一些C ++代码,我想知道它的具体标准版本是否(以及如何),以及它的C标准版本(如果有的话)是否相关。

I have verfied that this test code 我已经验证了这个测试代码

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

prints "200809" when compiled with any of "g++ -std=c++98", "g++ -std=c++11", "clang++ -std=c++98", "clang++ -std=c++11". 使用“g ++ -std = c ++ 98”,“g ++ -std = c ++ 11”,“clang ++ -std = c ++ 98”,“clang ++ -std = c ++”编译时打印“200809” 11" 。

(When I compile C with any explicit standards version, the _POSIX_C_SOURCE macro isn't defined at all). (当我使用任何显式标准版本编译C时,根本没有定义_POSIX_C_SOURCE宏)。

Why is that? 这是为什么? What doesn't make sense at all is that compiling C++98 effects in _POSIX_C_SOURCE being 200809 (that is, 10 years later ). 完全没有意义的是,在_POSIX_C_SOURCE中编译C ++ 98效果为200809(即10年 )。

There's two things that you might be looking for: 您可能正在寻找两件事:

  • If you want to detect C++98: The macro __cplusplus is defined to be 199711L . 如果要检测C ++ 98:宏__cplusplus定义为199711L
  • If you want to detect C++11: The macro __cplusplus is defined to be 201103L . 如果要检测C ++ 11:宏__cplusplus定义为201103L

If you'd like to detect compiler versions, this site has a ton of information about the various macros that apply: http://sourceforge.net/p/predef/wiki/Compilers/ 如果您想检测编译器版本,该站点有大量有关适用的各种宏的信息: http//sourceforge.net/p/predef/wiki/Compilers/

As to _POSIX_C_SOURCE , this is a attribute of the features available in the C Standard Library . 对于_POSIX_C_SOURCE ,这是C标准库中可用功能的属性。 So because you are using a new glibc (atleast 2.10), you are able to support these features. 因此,因为您正在使用新的glibc(至少2.10),所以您可以支持这些功能。

As to the C compiler not reporting these values, you may need to explicitly include <features.h> to access them. 对于不报告这些值的C编译器,您可能需要显式包含<features.h>来访问它们。

Well I think that's because _POSIX_C_SOURCE does not relate to any C++ standard spec, but to POSIX specs: 嗯,我认为这是因为_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).

The value you get is the default value supported by the compiler/libs you use. 您获得的值是您使用的编译器/库支持的默认值。

_POSIX_C_SOURCE may be a compiler extension. _POSIX_C_SOURCE可能是编译器扩展。

It is a POSIX spec, not a C++ spec. 它是POSIX规范,而不是C ++规范。

so some compiler won't support it. 所以有些编译器不支持它。

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

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