简体   繁体   English

C语言中#line的含义是什么?

[英]What's the meaning of #line in C language?

What's the meaning of #line in the C language? C语言中#line的含义是什么? Where would it be used? 它会在哪里使用?

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. 通常只有C预处理器添加这些,例如,当包含文件时,它告诉编译器(基本上只看到一个数据流)我们正在查看不同的文件。

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 . 这可能听起来很奇怪,但是预处理器只是在您指定包含的地方插入头文件,并且编译器可以处理整个事物(所有头文件与源代码连接在一起),如果使用,您可以检查预处理器阶段的结果gcc与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) 在那里你会注意到它会在你包含文件时添加一个#line指令,并且每当它减少了输入到编译器的文本量时(例如大量的注释可以减少到单个#line指令,跳过前面)

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. 它也被其他程序使用,比如bison / yacc告诉你问题(如果有编译问题)与你在特定行的规则文件有关,否则编译器将无法做到这一点,因为bison / yacc生成c文件。

It is called the preprocessor line control directive . 它被称为预处理器行控制指令

The expansions of both __FILE__ and __LINE__ are altered if a #line directive is used. 如果使用#line指令,则会更改__FILE____LINE__的扩展。 It causes the compiler to view the line number of the next source line as the specified number. 它使编译器将下一个源行的行号视为指定的数字。

Its main use is to make the compiler provide more meaningful error messages. 它的主要用途是使编译器提供更有意义的错误消息。

You can find more explanation and a usage example in IBM's documentation . 您可以在IBM的文档中找到更多解释和用法示例。

It is a pragma keyword: 它是一个pragma关键字:

" #line lets you modify the compiler's line number and (optionally) the file name output for errors and warnings. This example shows how to report two warnings associated with line numbers. The #line 200 directive forces the line number to be 200 (although the default is #7) and until the next #line directive, the filename will be reported as "Special". The #line default directive returns the line numbering to its default numbering, which counts the lines that were renumbered by the previous directive." #line允许您修改编译器的行号和(可选)文件名输出以查找错误和警告。此示例显示如何报告与行号相关的两个警告。#line 200指令强制行号为200(尽管默认值是#7)并且直到下一个#line指令,文件名将被报告为“Special”。#line default指令将行编号返回到其默认编号,该编号计算由前一个指令重新编号的行。 “

It allows you to change the apparent line number of the file. 它允许您更改文件的表观行号。

The only use I can think of for it is to make the line numbers sane after a long series of multi-line macros. 我能想到的唯一用途就是在一系列多行宏之后使行号保持正常。

usage is: 用法是:

#line 42

It is mostly used to supply the file names and line numbers of a source file from which a C file (be it header or implementation) was created. 它主要用于提供源文件的文件名和行号,从中创建C文件(标题或实现)。 Given that, the compiler would emit diagnostics that hint at the source file rather than at the generated file. 鉴于此,编译器将发出暗示源文件而不是生成文件的诊断信息。

Preprocessors also use this to hint at included headers in a preprocessed file that has these expanded. 预处理器还使用它来提示已经扩展的预处理文件中的包含标头。

#是处理器c和c ++的符号字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM