How can you tell whether a program was compiled using C versus C++?
The ANSI standard for the C language defines a symbol named _ _cplusplus that is defined only when you
are compiling a C++ program. If you are compiling a C program, the _ _cplusplus symbol is undefined.
Therefore, you can check to see whether the C++ compiler has been invoked with the following method:
#ifdef _ _cplusplus /* Is _ _cplusplus defined? */
#define USING_C FALSE /* Yes, we are not using C */
#else
#define USING_C TRUE /* No, we are using C */
#endif
When the preprocessor is invoked, it sets USING_C to FALSE if the _ _cplusplus symbol is defined. Otherwise,
if _ _cplusplus is undefined, it sets USING_C to TRUE. Later in your program, you can check the value of the
USING_C constant to determine whether the C++ compiler is being used.
are compiling a C++ program. If you are compiling a C program, the _ _cplusplus symbol is undefined.
Therefore, you can check to see whether the C++ compiler has been invoked with the following method:
#ifdef _ _cplusplus /* Is _ _cplusplus defined? */
#define USING_C FALSE /* Yes, we are not using C */
#else
#define USING_C TRUE /* No, we are using C */
#endif
When the preprocessor is invoked, it sets USING_C to FALSE if the _ _cplusplus symbol is defined. Otherwise,
if _ _cplusplus is undefined, it sets USING_C to TRUE. Later in your program, you can check the value of the
USING_C constant to determine whether the C++ compiler is being used.
Cross Reference:
V.18: What are the standard predefined macros?
V.19: How can a program be made to print the line number where an error occurs?
V.20: How can a program be made to print the name of a source file where an error occurs?
V.28: What are the _ _DATE_ _ and _ _TIME_ _ preprocessor commands?
No comments:
Post a Comment