範例 8

$4

這邊主要在說明如何增加 ./configure 的選項

Makefile.am

bin_PROGRAMS = hello

hello_SOURCES = hello.cc

configure.ac

AC_INIT(hello, 0.1, [email protected])
AC_CONFIG_SRCDIR([hello.cc])
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE([foreign])

AC_ARG_WITH(city, AS_HELP_STRING([--with-city=city], [City to greet (default is the whole world)]), [ case $withval in
        kaohsiung)
                AC_DEFINE_UNQUOTED(CITY, "高雄", "City name")
                ;;
        taipei)
                AC_DEFINE_UNQUOTED(CITY, "台北", "City name")
                ;;
        no)
                ;;
        *)
                AC_MSG_ERROR([City $withval is unknown])
                ;;
esac
])

AC_PROG_CXX

AC_CONFIG_FILES([
Makefile
])
AC_OUTPUT

hello.cc

#include "config.h"
#include <iostream>

using namespace std;

int main()
{
#ifdef CITY
        cout << "Hello, " CITY "!" << endl;
#else
        cout << "Hello, world!" << endl;
#endif
        return 0;
}

步驟

$ autoreconf -i
$ ./configure
$ make
$ ./hello 
Hello, world!
$ ./configure --with-city=taipei
$ make
$ ./hello
Hello, 台北!
$ ./configure --with-city=kaohsiung
$ make
$ ./hello
Hello, 高雄!

前一頁 | 往上 | 後一頁

last edited 2006-01-18 11:34:24 by FourDollars