GNU C attribute section的一个使用例子

本文介绍了一种使用GNU C编译器特性__attribute__((section("xx")))的方法,通过示例展示了如何将相关结构体组织到特定段中,并遍历这些段以查找所需信息。

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

1.前言:

    在内核中我们经常遇到"section"类似的字眼.把相关的CPU或设备信息存放在某个section里面.然后对这个section进行

遍历,判断这个section里面是否有我们自己想要的信息.这是一种不错的编译思想.下面整理了一个示例来加固下这种思想

的学习.


2.示例:

    car.h

   

[html]  view plain  copy
  1. #ifndef _CAR_H_  
  2. #define _CAR_H_  
  3.   
  4. typedef struct __car  
  5. {  
  6.   const char *name;  
  7.   const char *owner;  
  8.   int (*price)(void);  
  9. }car,*pcar;  
  10.   
  11. #ifndef MKSTR  
  12. #define MKSTR(x)    #x  
  13. #endif  
  14.   
  15. #define CAR_SECTION __attribute__((used,section("car")))  
  16. #define CAR(__name,__owner,__price) \  
  17.     static car car##__name CAR_SECTION = {  \  
  18.         .name = MKSTR(__name),\  
  19.         .owner = MKSTR(__owner),\  
  20.         .price = __price,\  
  21.         }  
  22.   
  23. #endif  

    car.c   

[html]  view plain  copy
  1. /*To Show How To Use GNU_C __attribute__(section("xx"))  
  2.  *Modify By SE7EN @2013-6-11  
  3.  */  
  4.   
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <string.h>  
  8. #include <getopt.h>  
  9.   
  10. #include "car.h"  
  11.   
  12. extern car __start_car,__stop_car;  
  13.   
  14. static const char *const short_option = "hqo:";  
  15. static const struct option long_option[] =  
  16. {  
  17.     {"help",0,NULL,'h'},  
  18.     {"quit",0,NULL,'q'},  
  19.     {"owner",1,NULL,'o'},  
  20.     {0,0,0,0}  
  21. };  
  22.   
  23. static void print_usage(FILE *stream,char *app_name,int exit_code)  
  24. {  
  25.     fprintf(stream,"Usage:\n");  
  26.     fprintf(stream,  
  27.         "-h --help Display This APP Usage Information As Follow:\n"  
  28.         "-q --quit Quit This APP.\n"  
  29.         "-o --owner Select Whose Car.For Example -o your/my/his/other.\n"  
  30.     );  
  31.   
  32.     exit(exit_code);  
  33. }  
  34.   
  35. int main(int argc,char **argv)  
  36. {  
  37.     int ret = -1;  
  38.     char *car_owner = NULL;  
  39.     pcar ptmp = NULL;  
  40.   
  41.     while((ret = getopt_long(argc,argv,short_option,long_option,NULL)) != -1)  
  42.     {  
  43.         switch(ret)  
  44.         {  
  45.             case 'h':  
  46.                     print_usage(stdout,argv[0],EXIT_SUCCESS);  
  47.                 break;  
  48.   
  49.             case 'o':  
  50.                     car_owner = optarg;  
  51.                 break;  
  52.   
  53.             case 'q':  
  54.                 exit(1);  
  55.                 break;  
  56.             default:  
  57.                 abort();  
  58.         }  
  59.     }  
  60.   
  61.     for(ptmp = &__start_car; ptmp != &__stop_car; ptmp++)  
  62.     {  
  63.         if(!(strcmp(ptmp->owner,car_owner)))  
  64.             {  
  65.                 printf("This Is %s-Car\n",ptmp->owner);  
  66.                 printf("This Car Named %s.\n",ptmp->name);  
  67.                 printf("The Price Of This Car Is %d.\n",ptmp->price());  
  68.             }  
  69.     }  
  70.   
  71.     return 0;  
  72. }  

    owner.c
[html]  view plain  copy
  1. #include "car.h"  
  2.   
  3. int his_car_price(void)  
  4. {  
  5.     return 5000000;  
  6. }  
  7.   
  8. int your_car_price(void)  
  9. {  
  10.     return 6000000;  
  11. }  
  12.   
  13. int other_car_price(void)  
  14. {  
  15.     return 7000000;  
  16. }  
  17.   
  18. int my_car_price(void)  
  19. {  
  20.     return 100;  
  21. }  
  22.   
  23. CAR(BMW,his,his_car_price);  
  24. CAR(Audi,your,your_car_price);  
  25. CAR(Benz,other,other_car_price);  
  26. CAR(Bike,my,my_car_price);  

Makefile

[html]  view plain  copy
  1. SRCS=$(wildcard *.c)  
  2. OBJS=$(SRCS:.c=.o)  
  3. CC=gcc  
  4. #INCLUDE=-I/xxx  
  5. #LIBS=-L/xxx  
  6. CCFLAGS= -g -Wall -O2  
  7.   
  8. car:$(OBJS)  
  9.     $(CC) $^ -o $@ #$(INCLUDE) $(LIBS)  
  10.   
  11. %.o:%.c  
  12.     $(CC) -c $< $(CCFLAGS)  
  13.   
  14. clean:  
  15.     rm -f *.o car  
  16.   
  17. .PHONY:  
  18.     clean  


编译运行测试:

[html]  view plain  copy
  1. root@seven-laptop:~/learn/gnu_c# make clean  
  2. rm -f *.o car  
  3. root@seven-laptop:~/learn/gnu_c# make  
  4. gcc -c car.c -g -Wall -O2  
  5. gcc -c owner.c -g -Wall -O2  
  6. gcc car.o owner.o -o car #   
  7. root@seven-laptop:~/learn/gnu_c# chmod +x car  
  8. root@seven-laptop:~/learn/gnu_c# ./car -h  
  9. Usage:  
  10. -h --help Display This APP Usage Information As Follow:  
  11. -q --quit Quit This APP.  
  12. -o --owner Select Whose Car.For Example -o your/my/his/other.  
  13. root@seven-laptop:~/learn/gnu_c# ./car -o your  
  14. This Is your-Car  
  15. This Car Named Audi.  
  16. The Price Of This Car Is 6000000.  
  17. root@seven-laptop:~/learn/gnu_c# ./car -o his  
  18. This Is his-Car  
  19. This Car Named BMW.  
  20. The Price Of This Car Is 5000000.  
  21. root@seven-laptop:~/learn/gnu_c# ./car -o other  
  22. This Is other-Car  
  23. This Car Named Benz.  
  24. The Price Of This Car Is 7000000.  
  25. root@seven-laptop:~/learn/gnu_c# ./car -o my  
  26. This Is my-Car  
  27. This Car Named Bike.  
  28. The Price Of This Car Is 100.  
  29. root@seven-laptop:~/learn/gnu_c#   

3.小结:

    通过GNU C的段来组织程序是一个很重要的思想,内核中也经常这样做.这样整理个实例来加深理解和感觉的培养.以后的工作编译中也可以借鉴一下这种编程手段.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值