Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ code and C version macros

Tags:

c++

c

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

like image 409
Jo So Avatar asked Mar 05 '14 13:03

Jo So


People also ask

What is macro code in C?

Macro in C programming is known as the piece of code defined with the help of the #define directive. Macros in C are very useful at multiple places to replace the piece of code with a single value of the macro. Macros have multiple types and there are some predefined macros as well.

What is macro in C with example?

A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. Here, when we use c in our program, it is replaced with 299792458 .

How do you declare a macro in C?

To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.

What type are macros in C?

In C, A macro is any constant value or variable with its value. And the macro name will get replaced by its value in the entire program. Macros help in writing less code and also in saving time.


2 Answers

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.

like image 87
Bill Lynch Avatar answered Oct 13 '22 00:10

Bill Lynch


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.

like image 29
neuro Avatar answered Oct 13 '22 00:10

neuro