Summary
1) #error
used to generate a compilation error message;
#warning
used to generate a compilation warning message;
2) means that the final executable program cannot be generated
3) Syntax: #error/#warning message
, the message does not need to be surrounded by double quotes
4) #line
used to specify the starting line number and file name of the subsequent code of
5) Syntax: #line line filename
filename winning rate, is a string (double quotes)
·#Error and #line analysis
1. #error and #warning
#error
used to generate a compilation error message;
#warning
used to generate a compilation warning message;
#error
is a precompiler pointer, which can be used for
prompt whether the compilation conditions are met.
Note: means that the final executable program cannot be generated
Syntax: #error message
, the message does not need to be surrounded by double quotes
#ifndef __cplusplus
#error This file should be processed with C++ compiler.
#endif
使用gcc编译器编译:gcc test.c
输出:test.c 4: error: #error This file should be processed with C++ compiler.
不会生成可执行程序
使用g++编译器编译:g++ test.c
可以生成可执行程序
分析:__cplusplus是C++编译器里特有的宏,
使用gcc时,无法识别这个宏,
因此输出了一条错误信息,并且由于存在错误信息,可执行程序无法生成;
使用g++时,就会跳过该段预处理。
#ifndef __cplusplus
#warning This file should be processed with C++ compiler.
#endif
使用gcc编译器编译:gcc test.c
输出:test.c 4: warning: #warning This file should be processed with C++ compiler.
虽然提示了警告,但是会生成可执行程序
2、#line
#line
used to forcibly specify a new line number and compiled file name, and renumber the source code; essentially redefine __LINE__ and __FILE__. Usage: #line number filename (filename can be omitted)
#line 1 "delphi.c" : 指定当前声明处的下一行为第1行,指定当前文件名为"delphi.c"
Background: At first, everyone wrote the code in one file. For example, three developers completed a .c file at the same time. At this time, it is difficult to locate who wrote the code. So use the #line preprocessing pointer to indicate the line number and file name of each piece of code. Next time there is a problem and an error is reported, you will know whose code is wrong.
This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。