Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ preprocessor

I'd rewritten a simple C++ program using unix as a variable name. But the program compilation failed.

#include <iostream>
int main() {
        int unix = 1;
        return 0;
}

After searching a lot on the internet I got to this website which helped me by saying that unix is predefined macro equal to 1.

I want to know list of all such predefined macros.

like image 454
John Avatar asked Oct 18 '10 10:10

John


People also ask

What is preprocessor in C?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What is preprocessor in C with example?

Preprocessing directives are lines in your program that start with # . The # is followed by an identifier that is the directive name. For example, #define is the directive that defines a macro. Whitespace is also allowed before and after the # .

What are the types of C preprocessor?

There are 4 Main Types of Preprocessor Directives:Macros. File Inclusion. Conditional Compilation. Other directives.

Which is a preprocessor?

In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers.


2 Answers

You can list all the predefined macros by using the GNU preprocessor cpp as:

cpp -dM file.cpp

Also note that macros such as unix, linux are non standard and you can disable them by using the -ansi compilation flag as:

g++ -ansi file.cpp

And you can use the -ansi flag with cpp also to get the list of all standard predefined macros:

cpp -dM -ansi file.cpp
like image 192
codaddict Avatar answered Oct 24 '22 07:10

codaddict


touch mysymdef.h; g++ -dM mysymdef.h It will generate a file mysymdef.h.gch which will have all predefined symbols/macros for you system. File is binary but with some edit it will work out.

for details refer to

http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation

http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html

like image 29
Rohit Avatar answered Oct 24 '22 08:10

Rohit