error的用法
- #error用于生成一个编译错误消息
用法
#error message
message不需要用双引号包围
- #error编译指示字用于自定义程序员特有的编译错误消息。类似的,#warning用于生成编译警告。
- #error是一种与编译器指示字
- #error可用于提示编译条件是否满足
#ifndef _cplusplus
#error This file should be processed with C++ compiler
#endif
编译过程中的任意错误信息意味着无法生成最终的可执行程序
示例代码:#error预处理初探
#include <stdio.h>
#ifndef __cplusplus
#error This file should be processed with C++ compiler.
#endif
class CppClass
{
private:
int m_value;
public:
CppClass()
{
}
~CppClass()
{
}
};
int main()
{
return 0;
}
当用C编译器编译时:
ctest.c:4: error: #error This file should be processed with C++ compiler.
ctest.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘CppClass’
示例代码:#error在条件编译中的应用
#include <stdio.h>
#define PRODUCT 1
void f()
{
#if ( PRODUCT == 1 )
printf("This is a low level product!\n");
#elif ( PRODUCT == 2 )
printf("This is a middle level product!\n");
#elif ( PRODUCT == 3 )
printf("This is a high level product!\n");
#endif
}
int main()
{
f();
printf("1. Query Information.\n");
printf("2. Record Information.\n");
printf("3. Delete Information.\n");
#if ( PRODUCT == 1 )
printf("4. Exit.\n");
#elif ( PRODUCT == 2 )
printf("4. High Level Query.\n");
printf("5. Exit.\n");
#elif ( PRODUCT == 3 )
printf("4. High Level Query.\n");
printf("5. Mannul Service.\n");
printf("6. Exit.\n");
#endif
return 0;
}
输出结果
This is a low level product!
1. Query Information.
2. Record Information.
3. Delete Information.
4. Exit.
#line的用法
#line用于强制指定新的行号和编译文件名,并对源程序的代码重新编号
用法:
filename 可省略
#line编译指示字的本质是重定义 LINE和 FILE
示例代码:#line的使用
#include <stdio.h>
// The code section is written by A.
// Begin
#line 1 "a.c"
// End
// The code section is written by B.
// Begin
#line 1 "b.c"
// End
// The code section is written by Delphi.
// Begin
#line 1 "delphi_tang.c"
int main()
{
printf("%s : %d\n", __FILE__, __LINE__);
printf("%s : %d\n", __FILE__, __LINE__);
return 0;
}
// End