1、
1)源代码:
test2.c
#line 1
int main(void){
printf("line 1\n");
printf("line 2\n");
printf("line 3\n");
printf("line 4\n");
printf("line 5\n");
printf("line 6\n");
printf("line 7\n");
printf("line 8\n");
#include "test1.c
test1.c
#line 9 "test2.c"
#define LINUX
#ifdef WIN32
printf("win32\n");
#elif defined LINUX
printf("linux %d %s\n",__LINE__,__FILE__);
#else
#error no flag define
//如果LINUX和WIN32没有定义,#error会显示错误信息,然后停止编译
#endif
}
2)运行结果:
mysea@mysea-pc:~/test$ ./test2
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
linux 13 test2.c
2、#error表示停止编译,显示错误信息
3、#line 直接指定下一行的行号及文件名
1)指定行号
#line n
2)指定文件名和行号
#line n "filename"
4、源代码的#include "test1.c"和#line 9 "test2.c"表示:test2.c和test1.c实质属于一个C程序:test2。
1)注意这只是标注,如果要在编译时把test1.c包括进来,必须加上#include "test1.c
2)编译时,编译test2
mysea@mysea-pc:~/test$ gcc -o test2 test2.c
本文详细介绍了C语言中预处理指令的使用方法,包括#error如何用于编译时错误提示,#line指令如何调整行号及文件名信息,以及源文件包含的正确方式。通过具体的示例代码展示了这些指令的应用场景。

被折叠的 条评论
为什么被折叠?



