Linux Makefile自动生成--config.h

转载于:https://blog.csdn.net/spch2008/article/details/12510805

Linux Makefile自动生成--总体流程
Linux Makefile自动生成--实例
Linux Makefile自动生成--config.h


config.h主要用于代码移植,产生可移植代码。

有些函数只适用于特定的系统,并不通用,如gettimeofday。只能在特定的系统上使用,这样就不能移植了。

可以在可以使用的系统上使用gettimeofday,而不能使用的系统上使用另一种方式。

1. 代码如下:

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <sys/time.h>  
  3. #include <time.h>  
  4.   
  5. #include "config.h"  
  6. double get_epoch()  
  7. {  
  8.     double sec;  
  9.   
  10.     #ifdef HAVE_GETTIMEOFDAY  
  11.         struct timeval tv;  
  12.         gettimeofday(&tv, NULL);  
  13.         sec = tv.tv_sec;  
  14.         sec += tv.tv_usec / 1000000.0;  
  15.     #else  
  16.         sec = time(NULL);  
  17.     #endif  
  18.   
  19.     return sec;  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     printf("%f\n", get_epoch());  
  25.   
  26.     return 0;  
  27. }  
上述config.h为生成的文件。通过#ifdef来采用某些代码。

2. autoscan

configure.scan内容如下:

[plain]  view plain  copy
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.   
  4. AC_PREREQ([2.68])  
  5. AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])  
  6. AC_CONFIG_SRCDIR([hello.c])  
  7. AC_CONFIG_HEADERS([config.h])  
  8.   
  9. # Checks for programs.  
  10. AC_PROG_CC  
  11.   
  12. # Checks for libraries.  
  13.   
  14. # Checks for header files.  
  15. AC_CHECK_HEADERS([sys/time.h])  
  16.   
  17. # Checks for typedefs, structures, and compiler characteristics.  
  18.   
  19. # Checks for library functions.  
  20. AC_CHECK_FUNCS([gettimeofday])  
  21.   
  22. AC_CONFIG_FILES([Makefile])  
  23. AC_OUTPUT  
可见,增多了AC_CHECK_HEADERS与AC_CHECK_FUNCS宏,用于检测系统是否支持该头文件与函数。不要忘记增加

AM_INIT_AUTOMAKE宏,修改如下:

[plain]  view plain  copy
  1. AC_PREREQ([2.68])  
  2. AC_INIT([main], [1.0], [BUG-REPORT-ADDRESS])  
  3. AC_CONFIG_SRCDIR([hello.c])  
  4. AC_CONFIG_HEADERS([config.h])  
  5. AM_INIT_AUTOMAKE(hello, 1.0)  
  6. # Checks for programs.  
  7. AC_PROG_CC  
  8.   
  9. # Checks for libraries.  
  10.   
  11. # Checks for header files.  
  12. AC_CHECK_HEADERS([sys/time.h])  
  13.   
  14. # Checks for typedefs, structures, and compiler characteristics.  
  15.   
  16. # Checks for library functions.  
  17. AC_CHECK_FUNCS([gettimeofday])  
  18.   
  19. AC_CONFIG_FILES([Makefile])  
  20. AC_OUTPUT  


3. autoheader

autoheader后形成config.h.in模板,而config.status根据此模板生成config.h。config.h.in部分内容如下:

[plain]  view plain  copy
  1. /* Define to 1 if you have the `gettimeofday' function. */  
  2. #undef HAVE_GETTIMEOFDAY  
  3.   
  4. /* Define to 1 if you have the <inttypes.h> header file. */  
  5. #undef HAVE_INTTYPES_H  
4. configure

config.h部分内容如下:

[plain]  view plain  copy
  1. #define HAVE_GETTIMEOFDAY 1  
  2.   
  3. /* Define to 1 if you have the <inttypes.h> header file. */  
  4. #define HAVE_INTTYPES_H 1  
5. 运行

[plain]  view plain  copy
  1. root@nova-controller:/home/spch2008/AutoMake# ./hello   
  2. 1381306762.538480  

注意:源文件要引入头文件config.h。这样,代码具有了可移植性。在生成Makefile前,检测系统环境,形成config.h头文件。


参考:http://www.lugod.org/presentations/autotools/presentation/autotools.pdf


猜你喜欢

转载自blog.csdn.net/junlon2006/article/details/80311710