Linux Makefile自動生成--config.h

原始出處: http://blog.csdn.net/spch2008/article/details/12510805


config.h主要用於代碼移植,產生可移植代碼。

有些函數只適用於特定的系統,並不通用,如gettimeofday。只能在特定的系統上使用,這樣就不能移植了。

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

1. 代碼如下:

  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
 print?
  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
 print?
  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
 print?
  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
 print?
  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
 print?
  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


發佈了12 篇原創文章 · 獲贊 44 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章