<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.在程序中了解自己的运行环境</span>
gcc扩展:
预定义宏:__FILE__,__LINE__,__DATE__,__TIME__,__FUNCTION__,(C++程序中会有CPLUSPLUS)
#pragma pack(1) 按1个字节对齐补齐(结构类型)
#pragma pack(4) 按4个字节对齐补齐(结构类型)
属性修饰__attribute__((属性))
函数constructor/destructor
<span style="font-size:18px;">#include<stdio.h>
void f1()__attribute__((constructor));
void f2()__attribute__((destructor));
int main()
{
puts("main function");
}
void f1()
{
puts("before main");
}
void f2()
{
puts("after main");
}</span>$ gcc -o main attribute.c && ./mainbefore main
main function
after main
结构的属性packed/ aligned(1) /aligned(4) 按几字节对齐补齐
#include <stdio.h>
typedef struct A{
char a;
double b;
char c;
}A;
typedef struct B{
char a;
double b;
char c;
}__attribute__((aligned(8))) B; // <span style="font-family: Arial, Helvetica, sans-serif;">__attribute__((packed))</span>
int main()
{
printf("sizeof double=%d\n", sizeof(double));
printf("sizof A=%d\n", sizeof(A));
printf("sizof B=%d\n", sizeof(B));
}
#define swap(x, y) { typeof(x) t=x; x=y; y=t; } //变量交换
工具命令
nm显示目标文件中的符合(名字)清单
objdump显示目标文件文件中的附件信息
ldd查看依赖的动态库
本文介绍了GCC编译器的一些扩展特性,包括预定义宏、结构体字节对齐设置及其实现方法,并通过示例代码展示了如何使用这些特性。
1386

被折叠的 条评论
为什么被折叠?



