gcc __attribute__  里有一个属性是 error 能够用于编译时报错。

參考:

https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html

error ("message")
If this attribute is used on a function declaration and a call to such a functionis not eliminated through dead code elimination or other optimizations, an errorwhich
will include message will be diagnosed. This is usefulfor compile time checking, especially together with
__builtin_constant_pand inline functions where checking the inline function arguments is notpossible through
extern 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 presence
of 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这个属性。

最新文章

  1. php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault
  2. openstack 上床镜像, 创建网络, 创建虚拟机 命令
  3. DNS服务器的配置与应用: BIND9 的安装与配置
  4. ubuntu14.04中安装QuartusII9.1步骤
  5. 20个经典bootsrtap后台html站点模板推荐
  6. Mysql数据库乱码与编码问题筛查
  7. 什么是Dubbo
  8. poj 2449 Remmarguts' Date 第k短路 (最短路变形)
  9. OPPO K3在哪里打开USB调试模式的完美方法
  10. 功能测试话题分享-0323 Bug
  11. shell 变量、参数、数组章节笔记
  12. Android Studio 关于沉浸式状态栏
  13. c++关键字extern的作用
  14. webpack笔记二——entry
  15. java web 程序---jsp连接mysql数据库的实例基础+表格显示
  16. leetcode-填充同一层的兄弟节点Ⅱ
  17. C# Pinvoke判断是UEFI模式还是BIOS模式
  18. 蓝桥杯 第三届C/C++预赛真题(6) 大数乘法(数学题)
  19. (转) Docker - Docker1.12服务发现,负载均衡和Routing Mesh
  20. 解决百度 ueditor v1.4.3 编辑器上传图片失真的bug?

热门文章

  1. OutputStreamWriter API 以及源码解读
  2. vue-router在IE11中页面不跳转
  3. Good Bye 2017 部分题解
  4. springMVC
  5. pfring破解DNA限制
  6. 利用gmpy2破解rsa
  7. saltstack 开发
  8. [BZOJ3924][ZJOI2015]幻想乡战略游戏(动态点分治)
  9. HDU 4493 Tutor (水题)
  10. CC1150 针对低功耗无线应用设计的高度集成多通道射频发送器