了解甚少的GNU C的__attribute__ 机制
平时忙着赶项目,很多东西都是不求甚解,当工作中遇到的一些比较刁钻的问题时,才发现自己和那些大牛的
差距---内功。熟练码农和码神的最大区别估计就是内功是否深厚了。在自我反思的过程中,也要逐渐的积累一些很
基本的知识,争取能烂熟于心,这样,才能做出高质量的产品。
最近遇到了一个__attribute__ 机制相关的问题,在这里栽了跟头,就要好好总结一下,免得在以后的岁月中,再
犯类似的错误。
其实,GNU C 的一大特色就是__attribute__ 机制。这种机制在嵌入式系统中应用十分广泛,它的神奇作用就表现
在三个地方:
设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute )。
我也就针对这三个属性做个总结吧。
函数属性(Function Attribute )
函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大。__attribute__
机制也很容易同非GNU应用程序做到兼容之功效。GNU CC需要使用 –Wall编译器来击活该功能,这是控制警告信息的一个很
好的方式。
格式:__attribute__ format
该__attribute__属性可以给被声明的函数加上类似printf或者scanf的特征,它可以使编译器检查函数声明和函数实际调用参数之间
的格式化字符串是否匹配。该功能十分有用,尤其是处理一些很难发现的bug。
example:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
m:第几个参数为格式化字符串(format string);
n:参数集合中的第一个,即参数“…”里的第一个参数在函数参数总数排在第几,注意,有时函数参数里还有“隐身”的呢,后面会提到;
源码实例参考:http://www.keil.com/support/man/docs/armcc/armcc_sam1436458756975.htm
http://unixwiz.net/techtips/gnu-c-attributes.html
变量属性(Variable Attribute )
编译器的关键字 __attribute__ 用来指定变量或结构位域的特殊属性。关键字后 双括弧中的内容是属性说明。常用的关键字都有
哪些呢?
section ("section-name")
packed
aligned (alignment)
举个例子:
在操作系统中常用的 __attribute__ ((section(
"name"
))) .....
就是通过着来实现的,这个的作用主要是:
编译生成的object file,代码会保存在".text"段,全局变量和静态变量会放在".data"和".bss"(未初始化的全局变量和局部静态变量)
利用GCC的扩展机制就可以做到自定义段了.
关于变量的还有其他例子,写的比较详细,需要可以参考:
http://www.cnblogs.com/respawn/archive/2012/07/09/2582078.html
http://www.cnblogs.com/astwish/p/3460618.html
类型属性(Type Attribute )
类型属性主要用在结构体的操作上,其实很多时候和变量属性结合使用的,下面的实例可以很好说明类型属性 的作用和使用方法:
代码:
struct p { int a; char b; short c; }__attribute__((aligned())) pp; struct m { char a; int b; short c; }__attribute__((aligned())) mm; struct o { int a; char b; short c; }oo; struct x { int a; char b; struct p px; short c; }__attribute__((aligned())) xx; int main() { printf("sizeof(int)=%d,sizeof(short)=%d.sizeof(char)=%d\n",sizeof(int),sizeof(short),sizeof(char)); printf("pp=%d,mm=%d \n", sizeof(pp),sizeof(mm)); printf("oo=%d,xx=%d \n", sizeof(oo),sizeof(xx)); return ; }
输出结果:
sizeof(int)=,sizeof(short)=.sizeof(char)= pp=,mm= oo=,xx=
了解甚少的GNU C的__attribute__ 机制的更多相关文章
- GNU C 扩展之__attribute__ 机制简介
在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了__attribute__相关语法及 ...
- GNU C __attribute__ 机制简介
摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了__attribute__相 ...
- __attribute__机制介绍
转载:http://blog.csdn.net/ithomer/article/details/6566739 1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__att ...
- __attribute__机制介绍 (转)
1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__attribute__机制. __attribute__可以设置函数属性(Function Attribute).变量 ...
- 【转】__attribute__机制介绍
1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__attribute__机制. __attribute__可以设置函数属性(Function Attribute).变量 ...
- __attribute__ 机制详解(一)
GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute).变量属性(Variable Attribute)和类型 ...
- __attribute__ 机制详解
GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute).变量属性(Variable Attribute)和类型 ...
- Linux内核入门(六)—— __attribute__ 机制【转】
转自:https://blog.csdn.net/yunsongice/article/details/5538020 GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__ ...
- __attribute__机制介绍(转)
转自 http://blog.csdn.net/ithomer/article/details/6566739 1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__att ...
随机推荐
- JAVA设计模式一策略模式(Strategy Pattern)
什么是设计模式? 就是一些经验.让程序代码更具弹性.好维护.代码复用的经验.而且设计模式都遵从一些OO设计原则. 题外话:以下罗列出常用的OO设计原则:链接 本文章介绍策略模式(Strategy Pa ...
- jQueryEasyUI学习笔记
data-options 是jQuery Easyui的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写入到html中 data-options="region:'w ...
- Sping Cloud hystrix.stream 自动发现-监控
相关组件安装脚本 [root@java_gateway4 java_tps]# cat cront_install.sh #!/bin/bashyum install jq -ymkdir /home ...
- ucore-lab1-练习5report
实验5--实现函数调用堆栈跟踪函数 需要完成kdebug.c中函数print_stackframe的实现,可以通过函数print_stackframe来跟踪函数调用堆栈中记录的返回地址. 一.函数堆栈 ...
- 使用es6的then()方法封装jquery的ajax请求
使用场景: jsp页面中使用jquery的ajax请求比较频繁,以前vue框架的项目用过axios,所以就想着用then()封装一个公共请求的方法,这样每次请求就不用那么麻烦的写一大堆请求参数了. 示 ...
- 445. Add Two Numbers II 链表中的数字求和
[抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...
- 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表
[抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...
- [leetcode]48. Rotate Image旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- PHP脚本命令行执行成功,CRON无法执行故障解决记录
先来看看一个最简单的PHP文件(ip.php) <?php $myip = get_ip_cmd(); echo($myip); // get ip address function get_i ...
- 学习爬虫Scrapy遇到的错误
1. 这个问题是由于路径中含有中文,导致编码格式出错的问题, 查看错误方法,进入到ntpath.py文件中去,找到85行,然后,print 一下result_path,因为后面报出的错误,就是因为这段 ...