Are line-markers in C preprocessor output used by compiler?

Viewed 733

Are linemarkers(# 1 "donothing.cpp" 2) in the C preprocessor output used by the compiler to generate the assembler (.s) file?

I expect that they are used to produce compiler errors and debugging information. So I can safely turn these off(with -P) while trying to follow the flow of macros?

I am learning C++, and the C preprocessor. I read in many C++ tutorial that macros are generally difficult to write correctly and can have unexpected results. Here is a sample preprocessor output. The C Preprocessor: Preprocessor Output

$ g++ -Wall -std=c++11 -E donothing.cpp
# 1 "donothing.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 391 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "donothing.cpp" 2

int main()
{
    return 0;
}
$ g++ -Wall -std=c++11 -E donothing.cpp -P
int main()
{
    return 0;
}

Is looking at preprocessor output the way to debug compiler errors? How else do c/c++ developers debug their macros (since I expect people work with multiple .cpp files very frequently and there are always compiler errors)?

1 Answers

Line number directives have two primary purposes:

  1. If the compiler encounters an error, it uses the most recent line number directive to determine what file and line to reference in the error message.

    (The #line directive can even be used in generated code to allow error messages to point directly to the original source file, rather than to an intermediate C source file.)

  2. If debugging information (-g) is turned on, line number data is included in debug sections of the generated object file.

Neither of these purposes is critical. If you're looking at assembler output and the line number directives are getting in your way, feel free to turn them off.

Related