GCC __attribute__ 选项

GCC __attribute__ 选项



参考资料:

__attribute__ 可以用来设置 Function-Attributes函数属性, Variable-Attributes变量属性 和 Type-Attributes类型属性, 只与声明一起使用. 即使函数是定义在同一文件内, 也需要额外提供声明才能生效. 写法为 __attribute__((arg1, arg2, arg3...)), 这里列举一些用比较常用的属性:

  • format

format 被用来描述函数的参数形式, 支持的参数形式有 printf, scanf, strftime 和 strfmon, 比较常用的是printf形式, format的语法为:

__attribute__((format(archetype, string-index, first-to-check)))

archetype为参数形式, string-index是指函数的格式化字符串在所有参数中所处的位置, first-to-check是指第一个格式化参数的位置, 例如

void myprint(const char *fmt, ...) \_\_attribute\_\_((format(printf, 1, 2)));

以上声明表示 myprint() 函数的第一个参数为格式化字符串, 第二个及往后的都是格式化参数, 这样以下调用在gcc -Wformat 选项中可能会产生警告:

myprint("%s", 6);
// warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

myprint("%d", 7);   // ok

myprint("%d%s%d", 7, 3);
// test.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
// test.c:10: warning: too few arguments for format [/code]
  • const

const 属性告诉编译器该函数调用一次后缓存返回结果,以后的调用直接使用缓存的值

  • noreturn

例如以下代码使用-Wall选项编译时会产生警告: warning: control reaches end of non-void function

extern void myexit{exit();}
int test(int n) {
    if ( n > 0 ) {
        myexit();  // 控制逻辑分支没有返回值
    }
    else return 0;
}

而将myexit()的声明改为:

extern void myexit(exit()) __attribute__((noreturn));

则不会产生上面的警告了.

  • aligned

设置类型所使用的对齐方式

#include <stdio.h>
struct a {
    char b;
    short c;
};

struct a4 {
    char b;
    short c;
} __attribute__((aligned(4)));

struct a8 {
    char b;
    short c;
} __attribute__((aligned(8)));

struct ap {
    char b;
    short c;
} __attribute__((packed));

int main() {
    printf("sizeof(char) = %d\n",sizeof(char));
    printf("sizeof(short) = %d\n",sizeof(short));
    printf("sizeof(a) = %d\n",sizeof(struct a));
    printf("sizeof(a4) = %d\n",sizeof(struct a4));
    printf("sizeof(a8) = %d\n",sizeof(struct a8));
    printf("sizeof(ap) = %d\n",sizeof(struct ap));
}

运行结果为

sizeof(char) = 1
sizeof(short) = 2
sizeof(a) = 4
sizeof(a4) = 4
sizeof(a8) = 8
sizeof(ap) = 3
  • no_instrument_function
    关于这个参数的使用首先要解释一下gcc的 -finstrument-functions 选项, 当GCC使用这个选项编译代码的时候会在每一个用户自定义函数中添加两个函数调用:

    void __cyg_profile_func_enter(void *this, void *callsite);
    void __cyg_profile_func_exit(void *this, void *callsite);

这两个函数是在glibc内部声明的, 可以由用户自己定义实现, __cyg_profile_func_enter() 在进入函数的时候调用, void __cyg_profile_func_exit() 在函数退出的时候调用. 第一个参数 this 指向当前函数地址, 第二个参数 callsite 指向上一级函数地址,举个例子来说明一下:

#include <stdio.h>

#define debug_print(fmt, args...) do { \
            fprintf(stderr, fmt, ##args); \
        }while(0)

void __cyg_profile_func_exit(void* callee, void* callsite) __attribute__((no_instrument_function));
void __cyg_profile_func_enter(void* callee, void* callsite) __attribute__((no_instrument_function));

void __cyg_profile_func_enter(void* callee, void* callsite) {
    debug_print("Entering %p in %p\n", callee, callsite);
}

void __cyg_profile_func_exit(void* callee, void* callsite) {
    debug_print("Exiting %p in %p\n", callee, callsite);
}

void foo() {
    printf("foo()\n");
}

int main() {
    foo();
    return 0;
}

编译运行:

gcc t.c -finstrument-functions -g
$ ./a.out
Entering 0x10dbbde50 in 0x7fff8a2df7e1
Entering 0x10dbbde00 in 0x10dbbde77
foo()
Exiting 0x10dbbde00 in 0x10dbbde77
Exiting 0x10dbbde50 in 0x7fff8a2df7e1

这里输出的都是函数地址信息,如果想定位到函数代码,可以借助addr2line工具:

$ addr2line -f -e a.out -s 0x8048522 0x804855c
foo
t.c:18
main
t.c:22

最后回到 __attribute__((no_instrument_function)), 这个选项的作用就是用来禁止编译器向指定的函数内部添加__cyg_profile_func_enter() 和 __cyg_profile_func_exit()调用代码, 例如用户可能会在__cyg_profile_func_enter 函数中调用自定义的函数, 这些被调用的自定义函数声明中都要加上__attribute__((no_instrument_function))属性, 避免产生无限递归的调用. 另外也可以通过GCC的以下两个选项来实现同样的作用:

-finstrument-functions-exclude-file-list=file1,file2,... #屏蔽file1,file2中的函数
-finstrument-functions-exclude-function-list=func1,func2,... #屏蔽func1,func2函数

另外可以参考一篇 developerWorks 的文章,用 Graphviz 可视化函数调用 使用 -finstrument-functions选项结合addr2line、graphviz 实现函数调用可视化

  • deprecated

设置了这一属性的函数/变量/类型在代码中被使用的时候会使编译器产生一条警告, 例如

void foo() __attribute__((deprecated));
void foo(){}

int main() {
    foo();
    return 0;
}

$ gcc test.c
test.c: In function ‘main’:
test.c:6: warning: ‘foo’ is deprecated (declared at test.c:3)

这个选项可以用来在检验代码升级的时候旧版本的代码是否都已经被移除.

  • constructor/destructor

constructor 修饰的函数将会在main()函数调用之前触发,而destructor修饰的函数则将会在main()函数退出或者exit()之后触发

  • weak

默认情况下,GCC将函数和已初始化的全局变量定义为强符号类型,未初始化的全局变量为弱符号类型。在没有强符号声明的情况下弱符号声明(如果存在)会生效。这个属性常常被用来在库函数中定义一些调试函数,在用户代码中可以被重写覆盖。例如有两个文件,库文件 libfoo.c:

void foo() {}
int main() __attribute__ ((weak));
int main() {
    printf("this is test code\n");
}

以及用户文件

void foo();
int main() {
    foo();
    printf("this is user code\n");
}

以上两份代码可以被分别编译运行,也可以被编译链接后运行,如果在foo.c中的main函数没有weak属性声明,则编译器在链接时会报错 "duplicated symbol"。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值