GNU Compiler Collection (GCC)は、CおよびC++のコンパイラである。フリーソフトウェアであり、UnixやMicrosoft Windowsなどさまざまなプラットフォームで使用できる。
LinuxでC言語のソースファイルをコンパイルする場合、gccコマンドを使用するのが一般的である。
gcc [options] path
gccには次のオプションを指定できる。
-c
$ gcc -c example.c
Position Independant Codeで作成する。共有ライブラリ(*.so)をコンパイルする際に指定する。
ヘッダファイル(インクルードファイル)を探すディレクトリを指定する。
出力するファイル名をfileとする。-oオプションを省略した場合、a.outというファイル名で作成する。
$ gcc -o example example.c
共有オブジェクト(*.so)を作成する。
次のgccオプションは、プリプロセッサに関するオプションである。
通常の入力ファイルが処理される前にfileを処理する。fileに含まれる内容は、一番最初にコンパイルされる。
通常の入力ファイルが処理される前にfileを処理するが、その結果の出力を捨てる。fileによって生成された出力は捨てられるため、「-include file」の処理結果の影響は、file内に記述されたマクロが入力ファイル内で使用可能になることだけである。
gccの-imacrosオプションを使用する例として、ソースプログラムhelloworld.cの内容を次に示す。
#include <stdio.h>
main() {
printf("%s\n", HELLO_WORLD);
}
マクロを定義したファイルhelloworld.hの内容を次に示す。
#define HELLO_WORLD "Hello, world!"
ソースプログラムの中で#include "helloworld.h"
を記述してインクルードすることもできるが、gccの-imacrosオプションを指定してコンパイルすることもできる。
$ gcc -imacros helloworld.h helloworld.c
次のgccオプションは、デバッグに関するオプションである。
gccでコンパイルする例を示す。
C言語のソースプログラムをコンパイルする。
$ gcc example.c
Linuxで静的ライブラリ(*.a)を作成する。
$ gcc -c example.c
$ ar rv libexample.a example.o
Linuxで共有ライブラリ(*.so)を作成する場合、-fPIC(Position Independant Code)オプションを指定する。
$ gcc -fPIC -shared example.c -o libexample.so
共有ライブラリとリンクした実行可能ファイルを実行するには、あらかじめ環境変数LD_LIBRARY_PATHに共有ライブラリを配置したディレクトリのパスを設定しておく必要がある。
環境変数LD_LIBRARY_PATHにパスを設定していないと、実行可能ファイルを実行した際に以下のエラーが出力される。
$ ./samplelmf
./samplelmf: error while loading shared libraries: libsample.so: cannot open shared object file: No such file or directory
UbuntuやDebianなど、debパッケージ管理システムのLinuxの場合、aptコマンドでgccをインストールする。
gccのインストールにはスーパーユーザ権限が必要なので、sudoコマンドでaptを実行する。
$ sudo apt install gcc
[sudo] password for marina:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-7 gcc-7 gcc-7-base libasan4 libatomic1 libbinutils
libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libgcc-7-dev libgomp1 libisl19 libitm1 liblsan0 libmpc3 libmpx2
libquadmath0 libtsan0 libubsan0 linux-libc-dev manpages-dev
Suggested packages:
binutils-doc cpp-doc gcc-7-locales gcc-multilib make autoconf automake libtool flex bison gdb gcc-doc gcc-7-multilib
gcc-7-doc libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan4-dbg liblsan0-dbg libtsan0-dbg libubsan0-dbg
libcilkrts5-dbg libmpx2-dbg libquadmath0-dbg glibc-doc
The following NEW packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-7 gcc gcc-7 gcc-7-base libasan4 libatomic1 libbinutils
libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libgcc-7-dev libgomp1 libisl19 libitm1 liblsan0 libmpc3 libmpx2
libquadmath0 libtsan0 libubsan0 linux-libc-dev manpages-dev
0 upgraded, 27 newly installed, 0 to remove and 0 not upgraded.
Need to get 26.9 MB of archives.
After this operation, 115 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Setting up libcc1-0:amd64 (8-20180414-1ubuntu2) ...
Setting up libtsan0:amd64 (8-20180414-1ubuntu2) ...
Setting up linux-libc-dev:amd64 (4.15.0-20.21) ...
Setting up liblsan0:amd64 (8-20180414-1ubuntu2) ...
Setting up gcc-7-base:amd64 (7.3.0-16ubuntu3) ...
Setting up binutils-common:amd64 (2.30-15ubuntu1) ...
Setting up libmpx2:amd64 (8-20180414-1ubuntu2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up libmpc3:amd64 (1.1.0-1) ...
Setting up libc-dev-bin (2.27-3ubuntu1) ...
Setting up manpages-dev (4.15-1) ...
Setting up libc6-dev:amd64 (2.27-3ubuntu1) ...
Setting up libitm1:amd64 (8-20180414-1ubuntu2) ...
Setting up libisl19:amd64 (0.19-1) ...
Setting up libasan4:amd64 (7.3.0-16ubuntu3) ...
Setting up libbinutils:amd64 (2.30-15ubuntu1) ...
Setting up libcilkrts5:amd64 (7.3.0-16ubuntu3) ...
Setting up libubsan0:amd64 (7.3.0-16ubuntu3) ...
Setting up libgcc-7-dev:amd64 (7.3.0-16ubuntu3) ...
Setting up cpp-7 (7.3.0-16ubuntu3) ...
Setting up binutils-x86-64-linux-gnu (2.30-15ubuntu1) ...
Setting up cpp (4:7.3.0-3ubuntu2) ...
Setting up binutils (2.30-15ubuntu1) ...
Setting up gcc-7 (7.3.0-16ubuntu3) ...
Setting up gcc (4:7.3.0-3ubuntu2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
$
Free Software Foundation, Inc. GCC, the GNU Compiler Collection