Linux Makefile自動生成--整體流程
Linux Makefile自動生成--實例
Linux Makefile自動生成--config.h函數
config.h主要用於代碼移植,產生可移植代碼。this
有些函數只適用於特定的系統,並不通用,如gettimeofday。只能在特定的系統上使用,這樣就不能移植了。.net
能夠在能夠使用的系統上使用gettimeofday,而不能使用的系統上使用另外一種方式。blog
1. 代碼以下:ip
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include "config.h"
double get_epoch()
{
double sec;
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
gettimeofday(&tv, NULL);
sec = tv.tv_sec;
sec += tv.tv_usec / 1000000.0;
#else
sec = time(NULL);
#endif
return sec;
}
int main(int argc, char* argv[])
{
printf("%f\n", get_epoch());
return 0;
}
上述config.h爲生成的文件。經過#ifdef來採用某些代碼。
2. autoscanget
configure.scan內容以下:it
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.io
AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])function
# Checks for programs.
AC_PROG_CC模板
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([sys/time.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CHECK_FUNCS([gettimeofday])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
可見,增多了AC_CHECK_HEADERS與AC_CHECK_FUNCS宏,用於檢測系統是否支持該頭文件與函數。不要忘記增長
AM_INIT_AUTOMAKE宏,修改以下:
AC_PREREQ([2.68])
AC_INIT([main], [1.0], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(hello, 1.0)
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([sys/time.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CHECK_FUNCS([gettimeofday])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
3. autoheader
autoheader後造成config.h.in模板,而config.status根據此模板生成config.h。config.h.in部份內容以下:
/* Define to 1 if you have the `gettimeofday' function. */
#undef HAVE_GETTIMEOFDAY
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
4. configure
config.h部份內容以下:
#define HAVE_GETTIMEOFDAY 1
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
5. 運行
root@nova-controller:/home/spch2008/AutoMake# ./hello
1381306762.538480
注意:源文件要引入頭文件config.h。這樣,代碼具備了可移植性。在生成Makefile前,檢測系統環境,造成config.h頭文件。
參考:http://www.lugod.org/presentations/autotools/presentation/autotools.pdf --------------------- 做者:spch2008 來源:CSDN 原文:https://blog.csdn.net/spch2008/article/details/12510805 版權聲明:本文爲博主原創文章,轉載請附上博文連接!