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 "[email protected]"
     #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

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