GUI プログラム入門 (3) Makefile を使ってみよう (Windows の話ではありません ) 西尾太 @Momonga Project
GUI プログラムは これまでのおさらい 初期化 無限ループ 終了処理 低レベルから高レベルまで Xlib Xt gtk+ 等々 言語も自由自在 C C++ C# VB.NET Python 等々 Shell Script でもできる zenity 使ってね
とりあえず hello_gtk.c #include <gtk/gtk.h> int main( int argc, char *argv[] ) { GtkWidget *window; GtkWidget *button; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); button = gtk_button_new_with_label ("Hello"); g_signal_connect (G_OBJECT (button), "clicked", gtk_main_quit, NULL); gtk_container_add (GTK_CONTAINER (window), button); gtk_widget_show (button); gtk_widget_show (window); gtk_main (); return 0; }
gcc でビルド $ gcc -o hello_gtk hello_gtk.c hello_gtk.c:1:21: error: gtk/gtk.h: そのようなファイルやディレクトリはありません hello_gtk.c: In function main : hello_gtk.c:5: error: GtkWidget undeclared (first use in this function) hello_gtk.c:5: error: (Each undeclared identifier is reported only once hello_gtk.c:5: error: for each function it appears in.) hello_gtk.c:5: error: window undeclared (first use in this function) hello_gtk.c:6: error: button undeclared (first use in this function) hello_gtk.c:9: error: GTK_WINDOW_TOPLEVEL undeclared (first use in this function) hello_gtk.c:12: error: gtk_main_quit undeclared (first use in this function) hello_gtk.c:12: error: NULL undeclared (first use in this function) 当然失敗
pkg-config を使う $ pkg-config gtk+-2.0 --cflags -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/ lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 $ pkg-config gtk+-2.0 --libs -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpng12 -lm -lpangocairo-1.0 -lgio-2.0 -lxext -lxinerama -lxi -lxrandr -lxcursor -lxcomposite -lxdamage -lpangoxft-1.0 -lpangox-1.0 -lxfixes -lcairo -lpangoft2-1.0 -lxft -lxrender -lx11 -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 これは使える
pkg-config って何? ビルドするための設定値を出力します 設定ファイルはライブラリについてます /usr/lib/pkgconfig にあります /usr/share/pkgconfig にもあります PKG_CONFIG_PATH を調べます hoge.pc って名前です gtk+ では gtk+-2.0.pc という名前です 詳細は man 読め
gcc & pkg-config $ gcc -o hello_gtk hello_gtk.c $(pkg-config gtk+-2.0 --cflags --libs) $./hello_gtk 実行できた!
make コマンドを使ってみる $ export CFLAGS=$(pkg-config gtk+-2.0 --cflags) $ export LDFLAGS=$(pkg-config gtk+-2.0 --libs) $ make hello_gtk cc -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpng12 -lm -lpangocairo-1.0 -lgio-2.0 -lxext -lxinerama -lxi -lxrandr -lxcursor -lxcomposite -lxdamage -lpangoxft-1.0 -lpangox-1.0 -lxfixes -lcairo -lpangoft2-1.0 -lxft -lxrender -lx11 -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 hello_gtk.c -o hello_gtk $./hello_gtk ビルド 実行できた
Makefile いらないじゃん w 代わりに Makefile.am を作りましょう ついでに configure.ac も作りましょう autoreconf で configure スクリプト./configure && make && make install
Makefile.am の書式 インストール先 _ カテゴリ bin_programs = hello_gtk include_headers = hoge.h 等々 識別子 _ カテゴリ hello_gtk_sources = hello_gtk.c hello_gtk_cflags = $(HG_CFLAGS) 等々
今回の Makefile.am $ cat Makefile.am bin_programs = hello_gtk hello_gtk_sources = hello_gtk.c hello_gtk_cflags = $(HG_CFLAGS) hello_gtk_ldadd = $(HG_LIBS) 非常にシンプル
autoscan してみる $ ls Makefile.am hello_gtk.c $ autoscan $ ls Makefile.am autoscan.log configure.scan hello_gtk.c あっという間に configure.ac の雛形が!
configure.scan の内容 $ cat configure.scan # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.63]) AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) AC_CONFIG_SRCDIR([hello_gtk.c]) AC_CONFIG_HEADERS([config.h]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES([Makefile]) AC_OUTPUT
configure.ac にする $ cat configure.ac AC_INIT([hello_gtk], [0.0.1], [futoshi@momonga-linux.org]) AM_INIT_AUTOMAKE([foreign]) AC_CONFIG_SRCDIR([hello_gtk.c]) AC_PROG_CC AC_PATH_PROG(PKG_CONFIG, pkg-config, no) PKG_CHECK_MODULES([HG], [gtk+-2.0]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT コメント 空白行の削除 ( 深い意味は無い ) AC_PREREQ の削除 ( 深い意味は無い ) AC_INIT の編集 ( 名前 バージョン メールアドレス ) AM_INT_AUTOMAKE 追加 (GNU じゃないから foreign) AC_PATH_PROG 追加 (pkg-config の存在を調べる ) PKG_CHECK_MODULES 追加 (gtk+-2.0 を調べる ) 非常に簡単
autoreconf を実行する $ ls Makefile.am autoscan.log configure.ac configure.scan hello_gtk.c $ rm autoscan.log configure.scan $ ls Makefile.am configure.ac hello_gtk.c $ autoreconf -i configure.ac:3: installing `./install-sh' configure.ac:3: installing `./missing' Makefile.am: installing `./depcomp' $ ls Makefile.am aclocal.m4 configure depcomp install-sh Makefile.in autom4te.cache configure.ac hello_gtk.c missing configure スクリプトができちゃった
configure で Makefile を作る $./configure --prefix=$home インストール先はとりあえず $HOME にしよう $./configure --help ヘルプが表示されます! しかも 機能がいっぱい! $ make $ make install インストールできちゃった!
Makefile を使ってみよう $ make clean ビルドされたファイルを削除 $ make uninstall インストールされたファイルを削除 $ make dist tar 玉を作る (hello_gtk-0.0.1.tar.gz) $ make distcheck tar 玉が使えるかどうかまで調査 他にもいろいろあります!
詳細は GCC Make info 読め man 読め ググれ Autoconf Automake info 読め ググれ pkg-config man 読め README 読め ググれ
Momonga Linux 6 では GCC 4.3.3 Make 3.81 Autoconf 2.63 Automake 1.11 pkg-config 0.23 7 月末頃リリース予定 (?) バージョン変更あり