[小技巧] gcc attribute error 属性小试
2024-09-26 20:55:30
gcc __attribute__ 里有一个属性是 error 能够用于编译时报错。
參考:
https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html
error ("
message")
will include message will be diagnosed. This is usefulfor compile time checking, especially together with
__builtin_constant_p
and inline functions where checking the inline function arguments is notpossible throughextern char [(condition) ? 1 : -1];
tricks. While it is possible to leave the function undefined and thus invokea link failure, when using this attribute the problem will be diagnosedearlier and with exact location of the call even in presenceof inlinefunctions or when not emitting debugging information.
写了一个简单的演示样例:
extern void foo(void) __attribute__((error("build failed"))); #define FOO 0
int main(void)
{
if(FOO == 0)
foo(); return 1;
}
这里因为 FOO == 0 推断成立,所以会在编译时候直接报以下的错误:
/tmp/helloworld.c: In function 'main':
/tmp/helloworld.c:7:6: error: call to 'foo' declared with attribute error: build failed
这里文件名称/行号和错误的信息 "build failed" 都会输出。
另外。内核的 BUILD_BUG() 这个宏也是用了GCC这个属性。
最新文章
- php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault
- openstack 上床镜像, 创建网络, 创建虚拟机 命令
- DNS服务器的配置与应用: BIND9 的安装与配置
- ubuntu14.04中安装QuartusII9.1步骤
- 20个经典bootsrtap后台html站点模板推荐
- Mysql数据库乱码与编码问题筛查
- 什么是Dubbo
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
- OPPO K3在哪里打开USB调试模式的完美方法
- 功能测试话题分享-0323 Bug
- shell 变量、参数、数组章节笔记
- Android Studio 关于沉浸式状态栏
- c++关键字extern的作用
- webpack笔记二——entry
- java web 程序---jsp连接mysql数据库的实例基础+表格显示
- leetcode-填充同一层的兄弟节点Ⅱ
- C# Pinvoke判断是UEFI模式还是BIOS模式
- 蓝桥杯 第三届C/C++预赛真题(6) 大数乘法(数学题)
- (转) Docker - Docker1.12服务发现,负载均衡和Routing Mesh
- 解决百度 ueditor v1.4.3 编辑器上传图片失真的bug?