GNU C/C++ __attributes__

本文详细介绍了GCC中使用__attribute__关键字描述函数、变量和数据类型属性的方法,包括voidnoreturnfun()__attribute__((noreturn))、__attribute__((format(archetype,string-index,first-to-check))):formatattribute等,帮助开发者优化代码。

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

转自:http://blog.sina.com.cn/s/blog_79382a9301013mnm.html

最近在看一些源代码,遇到了一些使用__attribute__修饰函数和变量的属性方面的代码,不是太了解,很是汗颜,再此做个总结:


GCC使用__attribute__关键字来描述函数,变量和数据类型的属性,用于编译器对源代码的优化。
描述函数属性的几个重要的关键字:

  • void noreturnfun() __attribute__((noreturn));//函数不会返回。
  • void centon() __attribute__((alias("__centon")));//设置函数别名,函数是__cencon,别名是centon.
  • void main_enter() __attribute__((constructor));//main_enter函数在进入main函数前调用
  • void main_exit() __attribute__((destructor));//main_exit函数在main函数返回后调用
  • void fun() __attribute__ ((noinline));//fun函数不能作为inline函数优化
  • void fun() __attribute__ ((section("specials”)));//将函数放到specials段中,而不是通常的text段中
  • no_instrument_function、constructor和destructor关键字主要用于剖析(profiling)源代码的。
  • __attribute__(format(archetype,string-index,first-to-check)): format attribute提供了依照printf, scanf, strftime, strfmon类型函数的参数格式对目标函数进行类型的检查.
  • __attribute__((weak)): weak symbol,弱符号. 若存在两个相同的全局符号时,会引发重定义错误. 如果使用weak attribute,则当weak symbol和non-weak symbol同时存在的时候,linker会使用non-weak symbol.若只有weak symbol存在的时候则只使用weak symbol.
  • __attribute__((deprecated)): deprecated,弃用. 如果在源文件在任何地方地方使用deprecated attribute函数,编译器将会发出警告.
  • __attribute__((aligned(ALIGNMENT))): 指定变量或结构体最小字节对齐数,以byte为单位.ALIGNMENT: 指定的字节对齐操作数. 
  • __attribute__((cleanup(cleanup_function)): 当一个变量的作用域消失时,便会执行后面的clean_function函数.
  • __attribute__((packed)): 使变量或者是结构体按照最小的对齐方式,对于变量是1byte对齐,对于字段,也就是field指bit对齐. 
上面只是对常见的一些属性操作的解释,对于其他的用法应当参照GCC提供的文档。

ny@wxsc07:~/new/dxp_test_bak - new$ make clean && make CFLAGS="-g" rm -f main.o src/generate_box_pairs.so src/info_partition.so src/output_bin.so src/get_org_info.so src/generate_box_pairs.o src/info_partition.o src/output_bin.o src/get_org_info.o dxp_test mpic++ -I ./include -I /tools/Xilinx/Vitis_HLS/2022.2/include/ -O3 -fPIC -c main.cpp -o main.o In file included from ./include/md_fix.h:5, from main.cpp:7: ./include/ppip_float.h:7: warning: "__always_inline" redefined 7 | #define __always_inline //__attribute__((always_inline)) inline | In file included from /usr/include/features.h:461, from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33, from /usr/include/stdio.h:27, from main.cpp:1: /usr/include/x86_64-linux-gnu/sys/cdefs.h:319: note: this is the location of the previous definition 319 | # define __always_inline __inline __attribute__ ((__always_inline__)) | mpic++ -I ./include -I /tools/Xilinx/Vitis_HLS/2022.2/include/ -O3 -fPIC -c src/generate_box_pairs.cpp -o src/generate_box_pairs.o In file included from ./include/md_fix.h:5, from src/generate_box_pairs.cpp:1: ./include/ppip_float.h:7: warning: "__always_inline" redefined 7 | #define __always_inline //__attribute__((always_inline)) inline | In file included from /usr/include/features.h:461, from /usr/include/x86_64-linux-gnu/c++/9/bits/os_defines.h:39, from /usr/include/x86_64-linux-gnu/c++/9/bits/c++config.h:528, from /usr/include/c++/9/bits/stl_algobase.h:59, from /usr/include/c++/9/deque:60, from /usr/include/c++/9/queue:60, from /tools/Xilinx/Vitis_HLS/2022.2/include/hls_stream.h:23, from ./include/md_fix.h:4, from src/generate_box_pairs.cpp:1: /usr/include/x86_64-linux-gnu/sys/cdefs.h:319: note: this is the location of the previous definition 31
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值