My compiler expands it to 199711L. What does that mean? I read that __cplusplus > 199711L signifies C++11. What are the possible expansions of this macro and what does it signify?
C++ – How __cplusplus Directive is Defined in Various Compilers
c++compiler-versionpreprocessor-directive
Related Solutions
extern "C"
doesn't really change the way that the compiler reads the code. If your code is in a .c file, it will be compiled as C, if it is in a .cpp file, it will be compiled as C++ (unless you do something strange to your configuration).
What extern "C"
does is affect linkage. C++ functions, when compiled, have their names mangled -- this is what makes overloading possible. The function name gets modified based on the types and number of parameters, so that two functions with the same name will have different symbol names.
Code inside an extern "C"
is still C++ code. There are limitations on what you can do in an extern "C" block, but they're all about linkage. You can't define any new symbols that can't be built with C linkage. That means no classes or templates, for example.
extern "C"
blocks nest nicely. There's also extern "C++"
if you find yourself hopelessly trapped inside of extern "C"
regions, but it isn't such a good idea from a cleanliness perspective.
Now, specifically regarding your numbered questions:
Regarding #1: __cplusplus will stay defined inside of extern "C"
blocks. This doesn't matter, though, since the blocks should nest neatly.
Regarding #2: __cplusplus will be defined for any compilation unit that is being run through the C++ compiler. Generally, that means .cpp files and any files being included by that .cpp file. The same .h (or .hh or .hpp or what-have-you) could be interpreted as C or C++ at different times, if different compilation units include them. If you want the prototypes in the .h file to refer to C symbol names, then they must have extern "C"
when being interpreted as C++, and they should not have extern "C"
when being interpreted as C -- hence the #ifdef __cplusplus
checking.
To answer your question #3: functions without prototypes will have C++ linkage if they are in .cpp files and not inside of an extern "C"
block. This is fine, though, because if it has no prototype, it can only be called by other functions in the same file, and then you don't generally care what the linkage looks like, because you aren't planning on having that function be called by anything outside the same compilation unit anyway.
For #4, you've got it exactly. If you are including a header for code that has C linkage (such as code that was compiled by a C compiler), then you must extern "C"
the header -- that way you will be able to link with the library. (Otherwise, your linker would be looking for functions with names like _Z1hic
when you were looking for void h(int, char)
5: This sort of mixing is a common reason to use extern "C"
, and I don't see anything wrong with doing it this way -- just make sure you understand what you are doing.
This was fixed about a month ago (for gcc 4.7.0). The bug report makes for an interesting read: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=1773
Best Answer
The 199711L stands for Year=1997, Month = 11 (i.e., November of 1997) -- the date when the committee approved the standard that the rest of the ISO approved in early 1998.
For the 2003 standard, there were few enough changes that the committee (apparently) decided to leave that value unchanged.
For the 2011 standard, it's required to be defined as 201103L, (again, year=2011, month = 03) again meaning that the committee approved the standard as finalized in March of 2011.
For the 2014 standard, it's required to be defined as 201402L, interpreted the same way as above (February 2014).
For the 2017 standard, it's required to be defined as 201703L (March 2017).
For the 2020 standard, the value has been updated to 202002L (February 2020).
For the 2023 standard, the value has been updated to 202302L (February 2023).
Before the original standard was approved, quite a few compilers normally defined it to
0
(or just an empty definition like#define __cplusplus
) to signify "not-conforming". When asked for their strictest conformance, many defined it to1
.I almost forgot to mention, but one more tidbit about ancient compilers: a few of the earliest versions of cfront (and probably a few others copying it) defined
c_plusplus
instead of__cplusplus
. I don't recall it's being defined to any meaningful value though.