After example of use of #line

C

Charles L

Can someone provide me of an example of the use of the #line preprocessor
directive? I am not 100% sure how you use it.

Charles L
 
I

Ivan Vecerina

Charles L said:
Can someone provide me of an example of the use of the #line preprocessor
directive? I am not 100% sure how you use it.

This directive is primarily of interest to code generators.
Such as, for example, GNU flex which generates a .c file from
a source file that contains a mix of flex-specific directives
and fragments of source code.

By adding #line directives in the generated code, the tool
can ensure that compiler diagnostics/error messages, and
even line numbers used by the debugger to step through code,
will refer to the correct line of the user-edited source
(instead of stepping through the generated code).

For a concrete example, best might be to try to use one
such code generator. E.g: http://www.gnu.org/software/flex/


Greetings,
Ivan
 
J

John Harrison

Charles L said:
Can someone provide me of an example of the use of the #line preprocessor
directive? I am not 100% sure how you use it.

Charles L

It very rare that you would want to use it. Normally it would be used by
programs which generate programs not by human programmers.

Nevertheless ...

#line 200
// now the compiler thinks its on line 200

#line 300 "whatsit.cpp"
// now the compiler thinks its on line 300 of a file called whatsit.cpp

This might affect the printing of error messages for instance.

john
 
J

Jack Klein

Can someone provide me of an example of the use of the #line preprocessor
directive? I am not 100% sure how you use it.

Charles L

As others have pointed out, it is often produced by code generation
tools that produce C or C++ code from some other sort of input, so
that compilation error messages will contain a reference to the user
written source file name and line.

It can also be useful in production code that logs errors to a file
for later examination.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top