用C语言#line是什么意思? [英] What's the meaning of #line in C language?

查看:97
本文介绍了用C语言#line是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 语言中的 #line 是什么意思?将在哪里使用?

What's the meaning of #line in the C language? Where would it be used?

推荐答案

它告诉编译器以下一行的实际来源.通常只有C预处理器添加这些内容,例如,当包含一个文件时,它告诉编译器(基本上只看到一个数据流)我们正在查看其他文件.

It tells the compiler where the following line actually came from. It's usually only the C preprocessor that adds these, for example, when including a file, it tells the compiler (which is basically only seeing one stream of data) that we're looking at a different file.

这听起来可能很奇怪,但是预处理器只是在您指定要包含的头文件中插入头文件,然后编译器就可以处理所有事情(所有头文件与源代码并置在一起),您可以检查预处理器的结果如果将gcc与 gcc -E myfile.c 一起使用,则进入阶段.在其中,您会注意到无论何时包含文件,它都会添加一个 #line 指令,并且每当它减少了馈入编译器的文本量(例如,大量注释可能会减少为一个注释)时,都会在其中添加一个 #line 指令. #line 指令,向前跳过)

This may sound strange, but the preprocessor simply inserts the header files where you specify your includes, and the compiler works on the whole thing (all header files concatenated along with your source code), you can check the result of the preprocessor stage if using gcc with gcc -E myfile.c. In there you'll notice it adds a #line directive whenever you include files, and also whenever it reduces the amount of text fed to the compiler (such as large amounts of comments may be reduced to a single #line directive, skipping ahead)

其他程序(例如bison/yacc)也使用它来告诉您问题(如果存在编译问题)与特定行上的规则文件有关,否则编译器将无法执行该操作,因为bison/yacc会生成c文件.

It is also used by other programs, such as bison/yacc to tell you that the problem (if there's a compile problem) is related to your rules-file at a specific line, which the compiler would otherwise be unable to do, as the bison/yacc generates c-files.

这篇关于用C语言#line是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆