gccでヘッダファイル・ライブラリのパスを指定する方法

 オープンソースコンパイルで失敗する原因のほとんどは、ライブラリorヘッダファイルが存在しない・ライブラリorヘッダファイルのパスが誤っているのいずれかになります。後者の場合自分でMakefileを修正しなくてはなりません。そこでgccのオプションに関する知識が必要になります。

デフォルトでのライブラリとヘッダファイルの読み込み

 簡単なプログラムのコンパイルでも-vオプションを付けると、以下のように普段は見えない多くの自動的に付与されたパラータが見られます。ここかヘッダファイルとライブラリに関する情報を読み取ってみます。

[root@new-host code]# gcc -v test.c
Using built-in specs.
Target: x86_64-redhat-linux
コンフィグオプション: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
スレッドモデル: posix
gcc バージョン 4.1.2 20080704 (Red Hat 4.1.2-54)
 /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/cc1 -quiet -v test.c -quiet -dumpbase test.c -mtune=generic -auxbase test -version -o /tmp/cc8r87l0.s
存在しないディレクトリ "/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../x86_64-redhat-linux/include" を無視します
#include "..." の探索はここから始まります:
#include <...> の探索はここから始まります:
 /usr/local/include
 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/include
 /usr/include
探索リストの終わり
GNU C version 4.1.2 20080704 (Red Hat 4.1.2-54) (x86_64-redhat-linux)
&#160; &#160; &#160; &#160; compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-54).
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=63718
Compiler executable checksum: d9259db5918a1d4098c2a59efd7b40ce
&#160;as -V -Qy -o /tmp/cceOGiz0.o /tmp/cc8r87l0.s
GNU assembler version 2.17.50.0.6-20.el5_8.3 (x86_64-redhat-linux) using BFD version 2.17.50.0.6-20.el5_8.3 20061020
&#160;/usr/libexec/gcc/x86_64-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 /tmp/cceOGiz0.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crtn.o

ヘッダファイルは

/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/include
/usr/include

ライブラリは

-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 
-L/lib/../lib64 
-L/usr/lib/../lib64 /

となっていることが分かります。

ヘッダファイル・ライブラリの指定

 オプションとして、 -Iでヘッダファイルを探すディレクトリを指定し、-Lでライブラリを探すディレクトリを指定となっています。コンパイル時にそれぞれ-I/tmp -L/tmpと指定すると

[root@new-host code]# gcc -v -I/tmp -L/tmp test.c
Using built-in specs.
Target: x86_64-redhat-linux
コンフィグオプション: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
スレッドモデル: posix
gcc バージョン 4.1.2 20080704 (Red Hat 4.1.2-54)
 /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/cc1 -quiet -v -I/tmp test.c -quiet -dumpbase test.c -mtune=generic -auxbase test -version -o /tmp/ccdJLNUO.s
存在しないディレクトリ "/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../x86_64-redhat-linux/include" を無視します
#include "..." の探索はここから始まります:
#include <...> の探索はここから始まります:
 /tmp
 /usr/local/include
 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/include
 /usr/include
探索リストの終わり
GNU C version 4.1.2 20080704 (Red Hat 4.1.2-54) (x86_64-redhat-linux)
        compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-54).
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=63718
Compiler executable checksum: d9259db5918a1d4098c2a59efd7b40ce
 as -V -Qy -o /tmp/cca9BElw.o /tmp/ccdJLNUO.s
GNU assembler version 2.17.50.0.6-20.el5_8.3 (x86_64-redhat-linux) using BFD version 2.17.50.0.6-20.el5_8.3 20061020
 /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o -L/tmp -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 /tmp/cca9BElw.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crtn.o

ヘッダファイルは

/tmp
/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/include
/usr/include

ライブラリは

-L/tmp
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 
-L/lib/../lib64 
-L/usr/lib/../lib64

で確かにオプションの指定が反映されているのが見て取れます。