简体   繁体   中英

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.

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".

(When I compile C with any explicit standards version, the _POSIX_C_SOURCE macro isn't defined at all).

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 ).

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 .
  • If you want to detect C++11: The macro __cplusplus is defined to be 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/

As to _POSIX_C_SOURCE , this is a attribute of the features available in the C Standard Library . So because you are using a new glibc (atleast 2.10), you are able to support these features.

As to the C compiler not reporting these values, you may need to explicitly include <features.h> to access them.

Well I think that's because _POSIX_C_SOURCE does not relate to any C++ standard spec, but to POSIX specs:

_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.

It is a POSIX spec, not a C++ spec.

so some compiler won't support it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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