configure.ac 范例

原始出处: http://blog.csdn.net/john_crash/article/details/50110481


检查头文件

AC_CHECK_HEADERS([headers]) 
例如:

AC_CHECK_HEADERS([unistd.h windows.h])
 
 
  • 1

这个宏将在当前建造环境下检查unistd.h,windows.h是否存在。并将两个参数写入到配置头文件中。一般是config.h,你可以使用AC_CONFIG_HEADERS([headers])来指定。

AC_CONFIG_HEADERS([config.h])
 
 
  • 1

如果存在就会出现在config.h中例如下面:

/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1

/* Define to 1 if you have the <windows.h> header file. */
#define HAVE_WINDOWS_H 1
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

检查函数

AC_CHECK_FUNC (function, [action-if-found], [action-if-not-found]) 
AC_CHECK_FUNCS (function…, [action-if-found], [action-if-not-found]) 
检查函数是否存在,如果存在执行动作action-if-found,没有发现执行动作action-if-not-found。 
如果你没给出action-if-found和action-if-not-found,在发现函数的时候回定义对应的变量,以HAVE_开头,函数的名称都转换成大写。例如:

AC_CHECK_FUNCS(perror gettimeofday clock_gettime memset socket getifaddrs freeifaddrs fork)
 
 
  • 1

如果发现clock_gettime将会定义变量#define HAVE_CLOCK_GETTIME 1在对应的配置头文件中。 
如果没发现将不会定义。但是也会有一个注释行/* #undef HAVE_CLOCK_GETTIME */

为configure增加选项

AC_ARG_WITH (package, help-string, [action-if-given], [action-if-not-given]) 
这个宏可以给configure增加–with-package这样模式的参数。很多软件都有可选项用来打开扩展功能,AC_ARG_WITH就是干这个的。它的第一参数给出扩展包的名称,出现在–with-后面。第二个参数给出一个参数说明,用在./configure –help中。[action-if-given]如果有该选项就被执行,[action-if-not-given]如果没有加这个选项就执行。 
例如:

AC_ARG_WITH([militant],
    [AS_HELP_STRING([--with-militant],
        [Enable militant API assertions])],
    [zmq_militant="yes"],
    [])

if test "x$zmq_militant" = "xyes"; then
    AC_DEFINE(ZMQ_ACT_MILITANT, 1, [Enable militant API assertions])
fi
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

AS_HELP_STRING([–with-militant], 
[Enable militant API assertions]) 
定义一个帮助字串,将在configure –help中被显示出来。 
它可以这么使用configure –width-militant,这导致zmq_militant=”yes”被执行,随后通过测试来定义一个变量ZMQ_ACT_MILITANT=1。 
AC_DEFINE(VARIABLE, VALUE, DESCRIPTION) 
这个宏会在AC_CONFIG_HEADERS定义的头文件中增加一个定义项。例如:

       /* DESCRIPTION */
#define VARIABLE VALUE 
 
 
  • 1
  • 2

另外使用AC_ARG_ENABLE宏可以为configure增加–enable-feature 或者 –disable-feature这样的选项。 
AC_ARG_ENABLE (feature, help-string, [action-if-given], [action-if-not-given]) 
如果configure中加了给定的选项,就执行action-if-given,否则执行action-if-not-given。 
例如:

AC_ARG_ENABLE([eventfd],
    [AS_HELP_STRING([--disable-eventfd], [disable eventfd [default=no]])],
    [zmq_enable_eventfd=$enableval],
    [zmq_enable_eventfd=yes])

if test "x$zmq_enable_eventfd" = "xyes"; then
    # Check if we have eventfd.h header file.
    AC_CHECK_HEADERS(sys/eventfd.h,
        [AC_DEFINE(ZMQ_HAVE_EVENTFD, 1, [Have eventfd extension.])])
fi
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

共享库和静态库

编译动态库或者静态库,你需要再你的configure.ac中加入下面的宏:

LT_PREREQ([2.4.0])
LT_INIT([disable-static win32-dll dlopen])
AC_PROG_LIBTOOL
 
 
  • 1
  • 2
  • 3

LT_PREREQ给出一个版本需求检查。LT_INIT可以实现一些配置,例如win32-dll允许建造动态库,disable-static默认关闭静态库的建造。默认动态库和静态库是同时打开的。 
AC_PROG_LIBTOOL检查libtool脚本。做完这些在你的configure中会增加一些选项–enable-static , –enable-shared。 
细节参数可以看:libtool help document

自定义的测试程序

AC_RUN_IFELSE (input, [action-if-true], [action-if-false], [action-if-cross-compiling = ‘AC_MSG_FAILURE’]) 
编译运行input程序,如果程序成功运行返回0,执行action-if-true,否则执行action-if-false。如果交叉编译打开,那么编译出来的代码不能在本机执行,这是其他的动作都不会执行,如果action-if-cross-compiling存在将被执行。 
另外这里的input必须是有一个宏指定的源代码。 
AC_LANG_PROGRAM (prologue, body) 
例如:

[AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
                      [[fputs (hw, stdout);]])])
 
 
  • 1
  • 2

将被展开为下面的代码:

     #define PACKAGE_NAME "Hello"
     #define PACKAGE_TARNAME "hello"
     #define PACKAGE_VERSION "1.0"
     #define PACKAGE_STRING "Hello 1.0"
     #define PACKAGE_BUGREPORT "bug-hello@example.org"
     #define PACKAGE_URL "http://www.example.org/"
     #define HELLO_WORLD "Hello, World\n"

     const char hw[] = "Hello, World\n";
     int
     main ()
     {
     fputs (hw, stdout);
       ;
       return 0;
     }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

下面看一个完整的例子:

AC_MSG_CHECKING([if TIPC is available and supports nonblocking connect])

AC_RUN_IFELSE(
    [AC_LANG_PROGRAM([[
            #include <stdlib.h>
            #include <string.h>
            #include <fcntl.h>
            #include <errno.h>
            #include <sys/socket.h>
            #include <linux/tipc.h>
        ]],[[
            struct sockaddr_tipc topsrv;
            int sd = socket(AF_TIPC, SOCK_SEQPACKET, 0);
            if (sd == -EAFNOSUPPORT) {
                return 1;
            }
            memset(&topsrv, 0, sizeof(topsrv));
            topsrv.family = AF_TIPC;
            topsrv.addrtype = TIPC_ADDR_NAME;
            topsrv.addr.name.name.type = TIPC_TOP_SRV;
            topsrv.addr.name.name.instance = TIPC_TOP_SRV;
            fcntl(sd, F_SETFL, O_NONBLOCK);
            if (connect(sd, (struct sockaddr *)&topsrv, sizeof(topsrv)) != 0) {
                if (errno != EINPROGRESS)
                    return -1;
            }
        ]])
    ],
    [libzmq_tipc_support=yes],
    [libzmq_tipc_support=no],
    [libzmq_tipc_support=no])

AC_MSG_RESULT([$libzmq_tipc_support])
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

AC_MSG_CHECKING和AC_MSG_RESULT共同显示一个检查信息。这些信息将显示在执行configure脚本时。 
上面的宏在编译执行完给定代码后,如何成功就执行libzmq_tipc_support=yes,这同样导致configure打印一个信息if TIPC is available and supports nonblocking connect : yes 
下面你可以使用libzmq_tipc_support来定义一个宏到头文件中。

        if test "x$libzmq_tipc_support" = "xyes"; then
            AC_DEFINE(ZMQ_HAVE_TIPC, 1, [Have TIPC support])
        fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值