範例 8
$4
這邊主要在說明如何增加 ./configure 的選項
Makefile.am
bin_PROGRAMS = hello hello_SOURCES = hello.cc
執行檔為 hello
原始碼為 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
中間多了一段
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 ])
可以發現到其中內嵌了 shell script 的語法。
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, 高雄!