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

<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查看依赖的动态库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值