網站首頁 編程語言 正文
一、#error 的用法
#error 用于生成一個編譯錯誤消息
用法
#error message,message不需要用雙引號包圍
#error 編譯指示字用于自定義程序員特有的編譯錯誤消息,類似的,#warning 用于生成編譯警告。
#error 是一種預編譯器指示字
#error 可用于提示編譯條件是否滿足
用法示例如下:
編譯過程中的任意錯誤信息意味著無法生成最終的可執行程序。
下面初探一下 #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++ 寫的,所以用 gcc 編譯器時,編譯會報錯,其中 #error 那個是我們自定義的錯誤。
再來看一段 #error 在條件編譯中的應用代碼:
test.c:
#include <stdio.h>
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");
#else
#error The macro PRODUCT is NOT defined!
#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");
#else
#error The macro PRODUCT is NOT defined!
#endif
return 0;
}
如果我們直接編譯,而不去定義宏,那么自定義的錯誤就會被觸發:
如果在編譯時把宏加上,就不會出現錯誤了,例如將 PRODUCT 定義為 3,可以在命令行輸入 gcc -DPRODUCT=3 test.c
二、#line 的用法
#line 用于強制指定新的行號和編譯文件名,并對源程序的代碼重新編號
用法
#line number filename,filename 可省略
#line 編譯指示字的本質是重定義 _LINE_ 和 _FILE_
下面看一段 #line 的使用代碼:
test.c:
#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 AutumnZe.
// Begin
#line 1 "AutumnZe.c"
int main()
{
printf("%s : %d\n", __FILE__, __LINE__);
printf("%s : %d\n", __FILE__, __LINE__);
return 0;
}
// End
下面為輸出結果:
可以看到,#line 指定了新的行號和編譯文件名。
三、小結
- #error 用于自定義一條編譯錯誤信息
- #warning 用于自定義一條編譯警告信息
- #error 和 #warning 常應用于條件編譯的情形
- #line 用于強制指定新的行號和編譯文件名
原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/123520105
相關推薦
- 2022-10-05 使用?Swift?Package?插件生成代碼的示例詳解_Swift
- 2023-03-16 Redis設置key的過期時間_Redis
- 2023-04-18 go實現服務優雅關閉的示例_Golang
- 2022-06-16 golang?gorm實現get請求查詢案例測試_Golang
- 2022-07-20 react?diff?算法實現思路及原理解析_React
- 2022-12-24 Kubernetes?kubectl中Pod創建流程源碼解析_云和虛擬化
- 2022-07-27 Golang的strings.Split()踩坑記錄_Golang
- 2023-04-24 React組件與事件的創建使用教程_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支