この記事では、Mac OS X環境上に、GNU開発ツール(コマンドライン版)をインストールする方法を紹介します。
開発環境が未整備であることの確認
Macの出荷状態、もしくはMac OS Xインストール直後の状態では、開発環境は含まれていないため、別途インストールする必要があります。
これからインストールを開始する前に、GNU開発ツール関連が未整備であることを確認しておきましょう。まず、GNU開発ツール(GCC: GNU Compiler Collection)のコントロールセンターでもある、gccドライバの実行を試みます。
$ gcc -bash: gcc: command not found
"command not found"と表示され、gccというコマンドは現時点で存在しないことが分かります。
次に、Cライブラリを利用する際に必要となる、各種インクルードヘッダーファイルをチェックしてみます。UNIX環境では、Cライブラリのヘッダーファイル群は、/usr/include/ディレクトリに格納される習慣があります。
$ ls -F /usr/ X11/ bin/ libexec/ share/ X11R6@ lib/ sbin/ standalone/
/usr/include/ディレクトリそのものが、現時点では存在していないことを確認できたところで、作業を開始します。
AppStoreからXcodeを入手
最初に、AppStore経由でMac専用の開発環境Xcode(無料)を入手しましょう(執筆時点でバージョン 4.5.2, ダウンロードサイズ 1.5GiB)。
AppStoreアプリケーションを起動し、検索窓で"xcode"と入力すると、目指すXcodeのアイコンが表示されます。
Xcodeのページに切り替わると、左端に"無料"のボタンが表示されますので、このボタンをクリックします(ダウンロードという名前のボタンは、本ページ上に設置されていないため注意)。
無料ボタンをクリックすると、"Appをインストール"というボタンに変化しますので、再度クリックすると、インストールが開始されます(サインインしていない場合は、Apple IDを入力)。
しばらくの間、ボタンは"インストール中"という表示に切り替わりますが、処理が完了すると"インストール済み"となります。/Applications/フォルダの中に、Xcodeアプリが登録されていますので、起動してください(Xcode バージョン 4.2までは、/Developer/Applications/フォルダに格納)。
コマンドラインツールのインストール
Xcodeインストール時には、コマンドライン版のGNU開発ツールが用意されていないため、別途ダウンロードする必要があります。
Xcodeを最初に起動すると、使用許諾書が表示されますので、同意(Agree)ボタンを押します。
状況によっては、上の画面のように追加コンポーネントを要求されますので、適宜インストールを実施します。
無事、Xcodeが起動したら、メニューバーからPreferencesを選択します。
設定項目には、左からGeneral, Behaviors, Fonts&Colors, Text Editing, Key Bindings, Downloads, Locationsと並んでいますが、右からふたつめの"Downloads"を選択します。
"Command Line Tools"と表示されている行のインストールボタンをクリックすると、GNU開発ツール・コマンドライン版がインストールされます。
開発環境の確認
GNU開発ツールが正常にインストールされたかどうか、コマンドラインで確認してみましょう。
$ gcc i686-apple-darwin11-llvm-gcc-4.2: no input file
先ほどは、"command not found"と表示されましたが、今度は"no input file"に変わっています。これは、ソースファイルが指定されていないことを示す、エラーメッセージです。
GCCのバージョン情報を表示させる、-v オプションを指定してみましょう。
$ gcc -v Using built-in specs. Target: i686-apple-darwin11 Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1 Thread model: posix gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
今回インストールされたGCCのバージョンは、4.2.1であることが分かります。最後に、/usr/ディレクトリを見てみましょう。
$ ls -F /usr/ X11/ bin/ lib/ llvm-gcc-4.2/ share/ X11R6@ include/ libexec/ sbin/ standalone/
新たに、/usr/include/ディレクトリが作成されています。その中身は、どうなっているでしょうか。
$ ls -F /usr/include/ AssertMacros.h iso646.h python2.7@ Availability.h isql.h ranlib.h AvailabilityInternal.h isqlext.h readline/ AvailabilityMacros.h isqltypes.h readpassphrase.h Block.h krb5/ regex.h CommonCrypto/ krb5.h removefile.h ConditionalMacros.h langinfo.h resolv.h DNSServiceDiscovery/ launch.h rpc/ FlexLexer.h lber.h rpcsvc/ MacTypes.h lber_types.h rune.h NSSystemDirectories.h ldap.h runetype.h TargetConditionals.h ldap_cdefs.h sandbox.h TargetConfig.h ldap_features.h sasl/ Xplugin.h ldap_schema.h sched.h _locale.h ldap_utf8.h search.h _structs.h ldif.h secure/ _types.h libc.h security/ _wctype.h libcharset.h semaphore.h _xlocale.h libexslt/ servers/ aio.h libgen.h setjmp.h alloca.h libkern/ sgtty.h apache2/ libpq/ signal.h apr-1/ libpq-events.h slapi-plugin.h ar.h libpq-fe.h spawn.h architecture/ libproc.h sql.h arpa/ libunwind.h sql3types.h asl.h libxml2/ sqlca.h assert.h libxslt/ sqlda-compat.h bitstring.h limits.h sqlda-native.h bsm/ localcharset.h sqlda.h bzlib.h locale.h sqlext.h c++/ mach/ sqlite3.h cache.h mach-o/ sqlite3ext.h cache_callbacks.h mach_debug/ sqltypes.h checkint.h machine/ sqlucode.h com_err.h malloc/ stab.h complex.h math.h standards.h copyfile.h membership.h stdbool.h cpio.h memcached/ stddef.h crt_externs.h memory.h stdint.h ctype.h menu.h stdio.h cups/ miscfs/ stdlib.h curl/ monetary.h strhash.h curses.h monitor.h string.h cxxabi.h mpool.h stringlist.h db.h nameser.h strings.h default_pager/ nc_tparm.h struct.h device/ ncurses.h@ sys/ dirent.h ncurses_dll.h sysexits.h disktab.h ndbm.h syslog.h dispatch/ net/ tar.h dlfcn.h net-snmp/ tcl.h@ dns.h netdb.h tclDecls.h@ dns_sd.h netinet/ tclPlatDecls.h@ dns_util.h netinet6/ tclTomMath.h@ dtrace.h netkey/ tclTomMathDecls.h@ ecpg_config.h nfs/ term.h ecpg_informix.h nl_types.h term_entry.h ecpgerrno.h nlist.h termcap.h ecpglib.h notify.h termios.h ecpgtype.h notify_keys.h tic.h editline/ ntsid.h tidy/ err.h objc/ time.h errno.h odbcinst.h timeconv.h eti.h openssl/ tk.h@ execinfo.h os/ tkDecls.h@ expat.h panel.h tkIntXlibDecls.h@ expat_external.h paths.h tkMacOSX.h@ fcntl.h pcap/ tkPlatDecls.h@ fenv.h pcap-bpf.h ttyent.h ffi/ pcap-namedb.h tzfile.h fmtmsg.h pcap.h ucontext.h fnmatch.h pexpert/ ulimit.h form.h pg_config.h unctrl.h fsproperties.h pg_config_manual.h unistd.h fstab.h pg_config_os.h unwind.h fts.h pgtypes_date.h util.h ftw.h pgtypes_error.h utime.h get_compat.h pgtypes_interval.h utmp.h getopt.h pgtypes_numeric.h utmpx.h glob.h pgtypes_timestamp.h uuid/ grp.h php/ vfs/ gssapi/ poll.h vis.h gssapi.h postgres_ext.h vproc.h hfs/ postgresql/ wchar.h histedit.h profile.h wctype.h i386/ protocols/ wordexp.h iconv.h pthread.h xar/ ifaddrs.h pthread_impl.h xlocale/ inttypes.h pthread_spis.h xlocale.h iodbcext.h pwd.h xpc/ iodbcinst.h python2.5@ zconf.h iodbcunix.h python2.6@ zlib.h
驚くほど多くのヘッダーファイルとサブディレクトリが用意されています。この中には、C言語では有名な stdio.h というヘッダーファイルも登場しますが、これから私達はこれらのファイルにお世話になることになります。