Is the preprocessor output file a valid C / C ++ file

I have 2 files like qwe.h

#ifndef QWE_H #define QWE_H //#include <iostream> int asd(); #endif 

qwe.cc

 #include "qwe.h" int asd() { std::cout<<"asdasd"; } 

Starting the preprocessor only as g++ -E qwe.cpp > op4 gives the following output

 # 1 "qwe.cpp" # 1 "<built-in>" # 1 "<command line>" # 1 "qwe.cpp" # 1 "qwe.h" 1 int asd(); # 2 "qwe.cpp" 2 int asd() { std::cout<<"asdasd"; } 

Should preprocessor output be a valid C / C ++ file? which means the statements "# int string int"

+6
source share
3 answers

GCC is making reasonable efforts to make the preprocessor output dump still valid source code. But the general answer is no, it is not intended for this purpose.

You can also pass the -P option along with -E to make it more readable and less human-readable. More spaces will be removed, and markers # 234 "blah.h" will not be displayed.

Should the preprocessor output be a valid c / C ++ file?

Most of what the preprocessor does is lexing or dividing the source into tokens. Then it expands the macros that generate tokens and spaces according to certain rules. Perhaps the macro generates tokens not separated by spaces. According to the standard, the preprocessor output displayed directly in the form of text, as a rule, cannot be divided into the correct markers.

GCC goes the extra mile to try to meet the expectation that the preprocessor output can be expressed as text by introducing more spaces, but in fact it is not very good to rely on and, of course, not be carried over to other compilers.

+5
source

These are line numbers used to track where files are included in other files, which is useful when generating compiler warnings and errors.

See this Wikipedia article.

+6
source

There is no language requirement for preprocessor output, which must be valid C ++, or even for the preprocessor output to be available. But the preprocessor, which usually uses g ++ (perhaps always?), Generates output, which itself can be compiled as C ++.

What does the statement # int string int mean?

Any line starting with # is a preprocessing directive. If the token following # is not recognized otherwise, the part of the line after # is not a directive. (Despite the name, the preprocessing directive is not a directive.) Faults are usually ignored.

Usually this is a preprocessor that processes all preprocessing directives, including non-directives, but in this case some lines starting with # are passed to the compiler itself, which knows to ignore them.

You can demonstrate this by feeding the preprocessor output to the compiler:

 g++ -E foo.cpp > foo_preprocessed.cpp g++ -c foo_preprocessed.cpp 

If foo.cpp has preprocessor directives, in particular #include directives, foo-preprocessed.cpp will probably be much larger - but the compiler can still handle it.

The language standard defines the preprocessor output in terms of tokens rather than tex, but it provides sufficient flexibility so that the text is a valid way of representing the desired sequence of tokens.

(All this applies to both C and C ++.)

0
source

Source: https://habr.com/ru/post/948943/


All Articles