背景
GCC中实现的C++标准库中__cplusplus是一个很常见的宏。
弄清这个宏之前,我们需要先明白编程语言和编译器之间的关系。C++是一门编程语言,它有其标准。而编译C++代码的编译器有多种,如常见的Gnu Compiler Collection、Clang、Visual C++ 2017 Community。不同的C++编译器需要遵守/支持C++的标准。
了解上述背景之后,我们来查看__cplusplus宏在C++标准的要求和编译器中的定义。
C++标准和编译器中的预定义宏
鉴于C++的标准文档需要收费,我们在cppreference中查看关于__cplusplus的标准信息。
# 标准规定支持该宏 __cplusplus denotes the version of C++ standard that is being used, expands to value L(until C++11), L(C++11), L(C++14), L(C++17), or L(C++20) (macro constant)
我使用的是Ubuntu系统,所以我们查看下GCC标准预定义宏。关于windows系统中__cplusplus宏定义的查看,可参考/Zc:__cplusplus(启用更新的 __cplusplus 宏
# GCC遵守C++标准支持该宏 __cplusplus This macro is defined when the C++ compiler is in use. You can use __cplusplus to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to __STDC_VERSION__, in that it expands to a version number. Depending on the language standard selected, the value of the macro is L for the 1998 C++ standard, L for the 2011 C++ standard, L for the 2014 C++ standard, L for the 2017 C++ standard, L for the 2020 C++ standard, or an unspecified value strictly larger than L for the experimental languages enabled by -std=c++23 and -std=gnu++23.
这里,我们知道__cplusplus宏的含义是“C++的版本“。接下来我们查看当前系统该宏的值是多少。
查看__cplusplus宏定义的值
参考Which C++ standard is the default when compiling with g++?,我们使用下面命令查看宏的值。
# 输出编译器预定义的宏,grep进行过滤 ➜ g++ -dM -E -x c++ /dev/null | grep -F __cplusplus #define __cplusplus L
关于GCC的命令行参数,可通过GCC手册查看。上面的参数含义如下:
-dM -E # 输出预定义的宏 Instead of the normal output, generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros. This gives you a way of finding out what is predefined in your version of the preprocessor. If you use ‘-dM’ without the ‘-E’ option, ‘-dM’ is interpreted as a synonym for ‘-fdump-rtl-mach’ -E # 预处理之后停止,预处理的结果输出到标准输出 Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files that don’t require preprocessing are ignored -x language # 指定语言 Specify explicitly the language
我们已经知道该宏的值,那这个宏在哪里/何时定义的呢?
__cplusplus宏定义的位置
结论:我暂时不知道,如果你知道的话,可以评论区留言。
我知道c++config用于预定义C++库的宏。但是编译器预定义的宏的位置在哪呢?(或者说,这些宏本身的创建过程是什么样子的?)
我下载了GCC源码,它使用autotools进行构建。我企图从这里找到答案。我使用关键字在源代码中进行搜素,但是并没有找到答案。
参考
Where do I find the current C or C++ standard documents?
What does the “__cplusplus” macro expand to?
Which C++ standard is the default when compiling with g++?
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218335.html原文链接:https://javaforall.net
