#line

guoxianyu521 2009-08-12 08:51:49
#line 他的用途是什么,请举出“实例”
...全文
85 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
满衣兄 2009-08-12
  • 打赏
  • 举报
回复
学习
taodm 2009-08-12
  • 打赏
  • 举报
回复
多试几个编译呗。
guoxianyu521 2009-08-12
  • 打赏
  • 举报
回复
那个代码,我看不懂;copy到vc上也运行不了。
lingyin55 2009-08-12
  • 打赏
  • 举报
回复
呃,上面不是有简单的代码例子么?直接msdn上copy的
guoxianyu521 2009-08-12
  • 打赏
  • 举报
回复
麻烦大家,给个简单的“程序”。谢谢!!!
yangyunzhao 2009-08-12
  • 打赏
  • 举报
回复
#line使得其后的源代码行从指定的符号常量整数值开始重新编号
如:
#line 100
使得下一行代码的行号从100开始
这种指令通常用于生成更有意义的语法错误消息和警告消息

参见http://topic.csdn.net/t/20030529/12/1849645.html
飞天御剑流 2009-08-12
  • 打赏
  • 举报
回复
#line用于改变当前文件名和行号

#line的语法如下:

#line number filename

例如:#line 30 a.h 其中,文件名a.h可以省略不写。

   这条指令可以改变当前的行号和文件名,例如上面的这条预处理指令就可以改变当前的行号为30,文件名为a.h,初看起来似乎没有什么用,不过,还是有点用的,编译器对C++源码编译过程中会产生一些中间文件,通过这条指令,可以保证文件名是固定的,不会被这些中间文件代替,有利于进行分析。
lingyin55 2009-08-12
  • 打赏
  • 举报
回复
The #line Directive
See Also Send Feedback


The #line directive tells the preprocessor to change the compiler's internally stored line number and filename to a given line number and filename.


#line digit-sequence ["filename"]


Remarks
The compiler uses the line number and optional filename to refer to errors that it finds during compilation. The line number usually refers to the current input line, and the filename refers to the current input file. The line number is incremented after each line is processed.

The digit-sequence value can be any integer constant. Macro replacement can be performed on the preprocessing tokens, but the result must evaluate to the correct syntax. The filename can be any combination of characters and must be enclosed in double quotation marks (" "). If filename is omitted, the previous filename remains unchanged.

You can alter the source line number and filename by writing a #line directive. The translator uses the line number and filename to determine the values of the predefined macros __FILE__ and __LINE__. You can use these macros to insert self-descriptive error messages into the program text. For more information on these predefined macros, see Predefined Macros.

The __FILE__ macro expands to a string whose contents are the filename, surrounded by double quotation marks (" ").

If you change the line number and filename, the compiler ignores the previous values and continues processing with the new values. The #line directive is typically used by program generators to cause error messages to refer to the original source file instead of to the generated program.

The following examples illustrate #line and the __LINE__ and __FILE__ macros.

In this statement, the internally stored line number is set to 151 and the filename is changed to copy.c.

Copy Code
#line 151 "copy.c"


In this example, the macro ASSERT uses the predefined macros __LINE__ and __FILE__ to print an error message about the source file if a given "assertion" is not true.

Copy Code
#define ASSERT(cond)

if( !(cond) )\
{printf( "assertion error line %d, file(%s)\n", \
__LINE__, __FILE__ );}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧