#error、#warning和#line

本文深入解析C++中#error、#line、#warning的使用方法,包括如何生成编译错误消息、警告及自定义行号。通过实例展示其在代码调试中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#error的用法

#error用于生成一个编译错误消息,并停止编译

用法          

#error  message

注:message不需要用双引号包围

#error编译指示字用于自定义程序员特有的编译错误消息

类似的,#warning用于生成编译警告,但不会停止编译


例1:

#include <stdio.h>

#define CONST_NAME1 "CONST_NAME1"
#define CONST_NAME2 "CONST_NAME2"

int main()
{  
    #ifndef COMMAND
    #warning Compilation will be stoped ...
    #error No defined Constant Symbol COMMAND
    #endif

    printf("%s\n", COMMAND);
    printf("%s\n", CONST_NAME1);
    printf("%s\n", CONST_NAME2);

    return 0;
}
编译报错,会显示我们自己定义的#warning和#error信息


#line的用法:

#line用于强制指定新的行号和编译的文件名,并对源程序的代码重新编号

用法

#line  number  filename

   注:filename可省略

#line编译指示字的本质是重定义内置宏__LINE__和__FILE__

例2:

test.c

#include <stdio.h>

#line 14 "Hello.c"  //告诉编译器,现在编译的文件名改为Hello.c了,然后从这一行的下一行开始是新指示的行,第4行变成了第14行,往后按新的行类推

#define CONST_NAME1 "CONST_NAME1"
#define CONST_NAME2 "CONST_NAME2"

void f()
{
    return 0;   //此处有警告,本来这一行是第10行,但是经过前面#line后,变成第20行了,所以警告信息会说在第20行
}

int main()
{
    printf("%s\n", CONST_NAME1);
    printf("%s\n", CONST_NAME2);
    printf("%d\n", __LINE__);
    printf("%s\n", __FILE__);
    
    f();

    return 0;
}
编译结果如下

注释掉函数f()中的return 0,在编译,会输出

可以看到__LINE__成了27,__FILE__成了Hello.c,原因就是前面的#line  14  "Hello.c"




.\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\port\heap_4.c: 1 warning, 1 error compiling port.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\port\port.c: 1 warning, 1 error compiling list.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\src\list.c: 1 warning, 1 error compiling croutine.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\src\croutine.c: 1 warning, 1 error compiling tasks.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\src\tasks.c: 1 warning, 1 error compiling queue.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\src\queue.c: 1 warning, 1 error compiling timers.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\src\timers.c: 1 warning, 1 error compiling event_groups.c... .\freertos\FreeRTOSConfig.h(83): warning: #47-D: incompatible redefinition of macro "configMAX_SYSCALL_INTERRUPT_PRIORITY" (declared at line 44) #define configMAX_SYSCALL_INTERRUPT_PRIORITY 5 /* equivalent to 0xb0, or priority 11. */ .\freertos\inc\portable.h(70): error: #35: #error directive: "Invalid portBYTE_ALIGNMENT definition" #error "Invalid portBYTE_ALIGNMENT definition" freertos\src\event_groups.c: 1 warning, 1 error compiling Delay.c... compiling OLED_Data.c... compiling TIM.c... system\TIM.c(86): warning: #1-D: last line of file ends without a newline // */ system\TIM.c: 1 warning, 0 errors compiling LED.c... compiling Key.c... compiling Serial.c... compiling OLED.c... Heardware\OLED.c(423): warning: #167-D: argument of type "volatile uint8_t *" is incompatible with parameter of type "uint8_t *" OLED_WriteData(OLED_DisplayBuf[j], 128); Heardware\OLED.c(464): warning: #167-D: argument of type "volatile uint8_t *" is incompatible with parameter of type "uint8_t *" OLED_WriteData(&OLED_DisplayBuf[j][X], Width); Heardware\OLED.c: 2 warnings, 0 errors compiling sys.c...
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值