The #line directive

本文详细介绍了C语言预处理器中的#line指令用法及其在源代码中的作用。通过使用#line指令,开发者可以调整编译器对于当前文件名及行号的认识,这对于由程序生成的源代码尤为重要,它能帮助生成更有意义的错误信息,并使得调试过程更加直观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

reference: https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Flc.htm

The #line directive

A preprocessor line control directive suppliesline numbers for compiler messages. It causes the compiler to view the linenumber of the next source line as the specified number.

Read syntax diagramSkip visual syntax diagram#line directive syntax
 
>>-#--line--+-decimal_constant--+-----------------+-+----------><
            |                   '-"--file_name--"-' |
            '-characters----------------------------'
 

In order for the compiler to produce meaningful references to line numbersin preprocessed source, the preprocessor inserts#line directiveswhere necessary (for example, at the beginning and after the end of includedtext).

A file name specification enclosed in double quotation marks can followthe line number. If you specify a file name, the compiler views the next lineas part of the specified file. If you do not specify a file name, the compilerviews the next line as part of the current source file.

At the C99 language level, the maximum value of the #line preprocessing directive is 2147483647.

In all C and C++ implementations, the tokensequence on a #line directive is subject to macro replacement.After macro replacement, the resulting character sequence must consist ofa decimal constant, optionally followed by a file name enclosed in doublequotation marks.

You can use #line control directives to make the compilerprovide more meaningful error messages. The following example program uses#line control directives to give each function an easily recognizableline number:

/**
 ** This example illustrates #line directives.
 **/

#include <stdio.h>
#define LINE200 200

int main(void)
{
   func_1();
   func_2();
}

#line 100
func_1()
{
   printf("Func_1 - the current line number is %d\n",_ _LINE_ _);
}

#line LINE200
func_2()
{
   printf("Func_2 - the current line number is %d\n",_ _LINE_ _);
}

This program produces the following output:

Func_1 - the current line number is 102
Func_2 - the current line number is 202



http://gcc.gnu.org/onlinedocs/cpp/Line-Control.html

6 Line Control

The C preprocessor informs the C compiler of the location in your sourcecode where each token came from. Presently, this is just the file nameand line number. All the tokens resulting from macro expansion arereported as having appeared on the line of the source file where theoutermost macro was used. We intend to be more accurate in the future.

If you write a program which generates source code, such as thebison parser generator, you may want to adjust the preprocessor'snotion of the current file name and line number by hand. Parts of theoutput from bison are generated from scratch, other parts comefrom a standard parser file. The rest are copied verbatim frombison's input. You would like compiler error messages andsymbolic debuggers to be able to refer to bison's input file.

bison or any such program can arrange this by writing‘#line’ directives into the output file. ‘#line’ is adirective that specifies the original line number and source file namefor subsequent input in the current preprocessor input file. ‘#line’ has three variants:

#line linenum
linenum is a non-negative decimal integer constant. It specifiesthe line number which should be reported for the following line ofinput. Subsequent lines are counted from linenum.
#line linenum filename
linenum is the same as for the first form, and has the sameeffect. In addition, filename is a string constant. Thefollowing line and all subsequent lines are reported to come from thefile it specifies, until something else happens to change that. filename is interpreted according to the normal rules for a stringconstant: backslash escapes are interpreted. This is different from‘ #include’.

Previous versions of CPP did not interpret escapes in ‘#line’;we have changed it because the standard requires they be interpreted,and most other compilers do.

#line anything else
anything else is checked for macro calls, which are expanded. The result should match one of the above two forms.

#line’ directives alter the results of the __FILE__ and__LINE__ predefined macros from that point on. See Standard Predefined Macros. They do not have any effect on ‘#include’'sidea of the directory containing the current file. This is a changefrom GCC 2.95. Previously, a file reading

     #line 1 "../src/gram.y"
     #include "gram.h"

would search for gram.h in ../src, then the -Ichain; the directory containing the physical source file would not besearched. In GCC 3.0 and later, the ‘#include’ is not affected bythe presence of a ‘#line’ referring to a different directory.

We made this change because the old behavior caused problems whengenerated source files were transported between machines. For instance,it is common practice to ship generated parsers with a source release,so that people building the distribution do not need to have yacc orBison installed. These files frequently have ‘#line’ directivesreferring to the directory tree of the system where the distribution wascreated. If GCC tries to search for headers in those directories, thebuild is likely to fail.

The new behavior can cause failures too, if the generated file is notin the same directory as its source and it attempts to include a headerwhich would be visible searching from the directory containing thesource file. However, this problem is easily solved with an additional-I switch on the command line. The failures caused by the oldsemantics could sometimes be corrected only by editing the generatedfiles, which is difficult and error-prone.


# # This is the main Apache HTTP server configuration file. It contains the # configuration directives that give the server its instructions. # See <URL:http://httpd.apache.org/docs/2.4/> for detailed information. # In particular, see # <URL:http://httpd.apache.org/docs/2.4/mod/directives.html> # for a discussion of each configuration directive. # # Do NOT simply read the instructions in here without understanding # what they do. They're here only as hints or reminders. If you are unsure # consult the online docs. You have been warned. # # Configuration and logfile names: If the filenames you specify for many # of the server's control files begin with "/" (or "drive:/" for Win32), the # server will use that explicit path. If the filenames do *not* begin # with "/", the value of ServerRoot is prepended -- so "logs/access_log" # with ServerRoot set to "/usr/local/apache2" will be interpreted by the # server as "/usr/local/apache2/logs/access_log", whereas "/logs/access_log" # will be interpreted as '/logs/access_log'. # # NOTE: Where filenames are specified, you must use forward slashes # instead of backslashes (e.g., "c:/apache" instead of "c:\apache"). # If a drive letter is omitted, the drive on which httpd.exe is located # will be used by default. It is recommended that you always supply # an explicit drive letter in absolute paths to avoid confusion. # # ServerRoot: The top of the directory tree under which the server's # configuration, error, and log files are kept. # # Do not add a slash at the end of the directory path. If you point # ServerRoot at a non-local disk, be sure to specify a local disk on the # Mutex directive, if file-based mutexes are used. If you wish to share the # same ServerRoot for multiple httpd daemons, you will need to change at # least PidFile. # Define SRVROOT "E:/Apache24" ServerRoot "${SRVROOT}" # # Mutex: Allows you to set the mutex mechanism and mutex file directory # for individual mutexes, or change the global defaul
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值