您的位置:首页 > 其它

在程序中了解自己的运行环境--笔记

2014-11-10 22:27 267 查看
<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 && ./main

before 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));
}


typeof (x) 在编译时取得x的类型名 
#define swap(x, y) { typeof(x) t=x; x=y; y=t; } //变量交换

工具命令

nm显示目标文件中的符合(名字)清单

objdump显示目标文件文件中的附件信息

ldd查看依赖的动态库
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐