GUIプログラム入門(3)
Makefileを使ってみよう
(Windowsの話ではありません)
これまでのおさらい
●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 ();
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.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.
configure.ac にする
コメント・空白行の削除(深い意味は無い) AC_PREREQ の削除(深い意味は無い) AC_INIT の編集(名前・バージョン・メールアドレス) AM_INT_AUTOMAKE 追加(GNUじゃないからforeign) AC_PATH_PROG 追加(pkg-configの存在を調べる) PKG_CHECK_MODULES 追加(gtk+-2.0を調べる)非常に簡単
$ cat configure.ac AC_INIT([hello_gtk], [0.0.1], [[email protected]]) AM_INIT_AUTOMAKE([foreign]) AC_CONFIG_SRCDIR([hello_gtk.c]) AC_PROG_CCAC_PATH_PROG(PKG_CONFIG, pkg-config, no) PKG_CHECK_MODULES([HG], [gtk+-2.0])
AC_CONFIG_FILES([Makefile]) AC_OUTPUT
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