第3回 GCCのC言語最適化以外のオプション

岸 哲夫

 前々回と前回とで,C言語の最適化オプションについて説明した.今回は,GCCのその他のオプション――出力の種類の制御オプション,C言語の“方言”を扱うオプション,ユーザープログラムまたはGCCをデバッグするためのオプション――について,説明と検証を行う. (筆者)

space
出力の種類の制御オプション

 コンパイルのプロセスは,四つに分けることができます.具体的には,前処理,コンパイル,アセンブル,リンクとなります.処理の実行は,この順番で行われます.

 前処理されたソースとは,#defineや#includeなどのマクロを展開したものです.それをコンパイルしたソースはアセンブラに展開されています.さらにそれをアセンブルしたものがオブジェクトファイルです.必要なオブジェクトファイルを結合してリンク処理が行われ,実行形式ファイルができます.

 前処理,コンパイル,アセンブルは個々のソースファイルに対して適用され,結果としてオブジェクトファイルが生成されます.リンクでは,指定されたすべてのオブジェクトファイルを組み合わせ,一つの実行形式ファイルを作ります.

 指定された入力ファイルごとに,ファイル名のサフィックス(拡張子)からコンパイルプロセスのどれが実行されるかが決定されます.

 以下に例をあげます.

  #gcc test01.c

 この場合,test01.cはCのソースファイルであり,前処理を必要とします.これを実行することでtest01.oが作成されます.

 そして,

  #gcc test01.i

とする場合,test01.iには#のついたマクロ文があってはなりません.このソースを前処理なしでコンパイル,アセンブル,リンクを行います.マクロ文が入っていると,以下のようなエラーを返します.

  undefined or invalid # directive

  #gcc test01.h

 このtest01.hはヘッダファイルです.この場合,前処理以降を行いません.文法エラーのチェックに使用することができます.

  #gcc test01.s

 このtest01.sはアセンブラのソースです.アセンブル,リンクを行います.

  #gcc test01.S(大文字のS)

 このtest01.Sはアセンブラのソースなのですが,少し特殊で前処理を行いコンパイルなしでアセンブル,リンクを行います.

 ここで,-xオプションについて説明します.後に続き入力された入力ファイルの言語を,ファイル名のサフィックスからコンパイラにデフォルトを選択させるのではなく,明示的に指定するこのオプションは,次に-xオプションが現れるまでの入力ファイルに対して適用されます.指定できる値は以下のとおりです.

  c objective-c c++
  c-header cpp-output c++-cpp-output
  assembler assembler-with-cpp none

 noneが指定された場合,言語を明示的に指定するのをとりやめます.これ以降に入力されたファイルは,ファイル名のサフィックスにしたがって処理されます.

 もちろん,C言語のソースなのにobjective-cと指定してもエラーになるだけです.

 コンパイルプロセスの一部分だけ実行したい場合は,-xで明示するか,ファイル名サフィックスの自動判断で開始する段階を指定することができます.どの処理で終了するかは,オプション-c,-S,-Eのどれか一つを使って指定します.組み合わせによって意味のない処理を行った場合,エラーにはならず,gccが何もしないこともあります.

 では,各オプションの説明を行います.

-c

 ソースファイルのコンパイルやアセンブルは行いますが,リンクは行いません.この場合は最終的にオブジェクトファイルを出力します.

 なおデフォルトでは,オブジェクトファイル名は,ソースファイル名のサフィックス.c,.i,.sなどを.oに置き換えたものになります.

-S

 コンパイル本体の段階で終了します.アセンブルは行いません.出力はアセンブラコードになります.

 なお,デフォルトでは,アセンブラファイル名は,ソースファイル名のサフィックス.c,.iなどを.sに置き換えたものになります.

-E

 前処理段階で終了します.コンパイラ本体は実行しません.出力は前処理されたソースコードの形式で,標準出力に出力されます.単純にマクロ展開されるだけです.

-o file

 出力をファイルfileに指定します.

 出力ファイルは一つしか指定できないので,複数の入力ファイルがある場合に-oを指定するのは,実行形式ファイルを出力とする場合にしか意味がありません.

 -oの指定がない場合,実行形式ファイルはa.outという名前で,source.suffixというソースファイルのオブジェクトファイルはsource.oという名前で,アセンブラファイルはsource.sという名前で作られます.

-v

 コンパイルの各段階で実行されるコマンドを標準エラー出力に表示します.また,コンパイラドライバプログラム,前処理,コンパイラ本体のバージョン番号も合わせて表示します.

  #gcc -v test01.c

 上記のコマンドを実行すると,リスト1のようになります.

〔リスト1〕gccが実行するコマンド
Reading specs from /usr/lib/gcc-lib/i586-pc-linux/2.95.3/specs
gcc version 2.95.3 20010315 (release)
 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MIN
OR__=95 -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__
linux__ -D__unix -D__linux -Asystem(posix) -Acpu(i386) -Amachine(i386) -Di386 -D
__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__
 test01.c /tmp/ccsYQdzP.i
GNU CPP version 2.95.3 20010315 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/include
 /usr/include
End of search list.
The following default directories have been omitted from the search path:
 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/../../../../include/g++-3
 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/../../../../i586-pc-linux/include
End of omitted list.
 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/cc1 /tmp/ccsYQdzP.i -quiet -dumpbase test
01.c -version -o /tmp/ccuwXQqw.s
GNU C version 2.95.3 20010315 (release) (i586-pc-linux) compiled by GNU C versio
n 2.95.3 20010315 (release).
 as -V -Qy -o /tmp/cc8hlIie.o /tmp/ccuwXQqw.s
GNU assembler version 2.11.90.0.23 (i586-pc-linux) using BFD version 2.11.90.0.2
3
 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/collect2 -m elf_i386 -dynamic-linker /lib
/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc-lib/i586-pc-linux/2.
95.3/crtbegin.o -L/usr/lib/gcc-lib/i586-pc-linux/2.95.3 /tmp/cc8hlIie.o -lgcc -l
c -lgcc /usr/lib/gcc-lib/i586-pc-linux/2.95.3/crtend.o /usr/lib/crtn.o

-pipe

 コンパイルの各段階間での情報の受け渡しに,一時ファイルではなく,パイプを使います.これはアセンブラがパイプからの読み出しができないシステムでは動作しませんが,GNUを使用していれば問題ありません.

--help

 gccが理解するコマンド行オプションの説明を標準出力に表示します.同時に-vオプションも指定すると,--helpはgccが起動するさまざまなプロセスにも渡されるので,渡されたプロセスは受け付け可能なコマンド行オプションを表示することができます.ここで,-Wオプションを一緒に指定すると,説明が用意されていないコマンド行オプションも表示されます.

  #gcc -v --help test01.c

 上記のコマンドを実行するとリスト2のようになります.

〔〔リスト2〕gccが実行するコマンドとそのhelp
Reading specs from /usr/lib/gcc-lib/i586-pc-linux/2.95.3/specs
gcc version 2.95.3 20010315 (release)

 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MIN
OR__=95 -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__
linux__ -D__unix -D__linux -Asystem(posix) -Acpu(i386) -Amachine(i386) -Di386 -D
__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__
 --help help-dummy /tmp/ccrDy0xn.i
GNU CPP version 2.95.3 20010315 (release) (i386 Linux/ELF)

 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/cc1 /tmp/ccrDy0xn.i -quiet -dumpbase help
-dummy.c -version --help -o /tmp/ccadprIA.s

 as -V -Qy --help -o /tmp/ccpxPSFN.o /tmp/ccadprIA.s
GNU assembler version 2.11.90.0.23 (i586-pc-linux) using BFD version 2.11.90.0.2
3

 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MIN
OR__=95 -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__
linux__ -D__unix -D__linux -Asystem(posix) -Acpu(i386) -Amachine(i386) -Di386 -D
__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__
 --help test01.c /tmp/ccrDy0xn.i
GNU CPP version 2.95.3 20010315 (release) (i386 Linux/ELF)

 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/cc1 /tmp/ccrDy0xn.i -quiet -dumpbase test
01.c -version --help -o /tmp/ccadprIA.s

 as -V -Qy --help -o /tmp/ccCox2d2.o /tmp/ccadprIA.s
GNU assembler version 2.11.90.0.23 (i586-pc-linux) using BFD version 2.11.90.0.2
3

 /usr/lib/gcc-lib/i586-pc-linux/2.95.3/collect2 -m elf_i386 -dynamic-linker /lib
/ld-linux.so.2 --help /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc-lib/i586-pc-l
inux/2.95.3/crtbegin.o -L/usr/lib/gcc-lib/i586-pc-linux/2.95.3 /tmp/ccpxPSFN.o /
tmp/ccCox2d2.o -lgcc -lc -lgcc /usr/lib/gcc-lib/i586-pc-linux/2.95.3/crtend.o /u
sr/lib/crtn.o
Usage: /usr/bin/ld [options] file...
Options:
  -a KEYWORD                  Shared library control for HP/UX compatibility
  -A ARCH, --architecture ARCH
                              Set architecture
  -b TARGET, --format TARGET  Specify target for following input files
  -c FILE, --mri-script FILE  Read MRI format linker script
  -d, -dc, -dp                Force common symbols to be defined
  -e ADDRESS, --entry ADDRESS Set start address
  -E, --export-dynamic        Export all dynamic symbols
  -EB                         Link big-endian objects
  -EL                         Link little-endian objects
  -f SHLIB, --auxiliary SHLIB Auxiliary filter for shared object symbol table
  -F SHLIB, --filter SHLIB    Filter for shared object symbol table
  -g                          Ignored
  -G SIZE, --gpsize SIZE      Small data size (if no size, same as --shared)
  -h FILENAME, -soname FILENAME
                              Set internal name of shared library
  -I PROGRAM, --dynamic-linker PROGRAM
                              Set the dynamic linker to use
  -l LIBNAME, --library LIBNAME
                              Search for library LIBNAME
  -L DIRECTORY, --library-path DIRECTORY
                              Add DIRECTORY to library search path
  -m EMULATION                Set emulation
  -M, --print-map             Print map file on standard output
  -n, --nmagic                Do not page align data
  -N, --omagic                Do not page align data, do not make text readonly
  -o FILE, --output FILE      Set output file name
  -O                          Optimize output file
  -Qy                         Ignored for SVR4 compatibility
  -q, --emit-relocs           Generate relocations in final output
  -r, -i, --relocateable      Generate relocateable output
  -R FILE, --just-symbols FILE
                              Just link symbols (if directory, same as --rpath)
  -s, --strip-all             Strip all symbols
  -S, --strip-debug           Strip debugging symbols
  -t, --trace                 Trace file opens
  -T FILE, --script FILE      Read linker script
  -u SYMBOL, --undefined SYMBOL
                              Start with undefined reference to SYMBOL
  --unique [=SECTION]         Don't merge input [SECTION | orphan] sections
  -Ur                         Build global constructor/destructor tables
  -v, --version               Print version information
  -V                          Print version and emulation information
  -x, --discard-all           Discard all local symbols
  -X, --discard-locals        Discard temporary local symbols (default)
  --discard-none              Don't discard any local symbols
  -y SYMBOL, --trace-symbol SYMBOL
                              Trace mentions of SYMBOL
  -Y PATH                     Default search path for Solaris compatibility
  -(, --start-group           Start a group
  -), --end-group             End a group
  -assert KEYWORD             Ignored for SunOS compatibility
  -Bdynamic, -dy, -call_shared
                              Link against shared libraries
  -Bstatic, -dn, -non_shared, -static
                              Do not link against shared libraries
  -Bsymbolic                  Bind global references locally
  --check-sections            Check section addresses for overlaps (default)
  --no-check-sections         Do not check section addresses for overlaps
  --cref                      Output cross reference table
  --defsym SYMBOL=EXPRESSION  Define a symbol
  --demangle [=STYLE]         Demangle symbol names [using STYLE]
  --demangler DSO:FUNCTION    Set DSO and demangler function
  --embedded-relocs           Generate embedded relocs
  -fini SYMBOL                Call SYMBOL at unload-time
  --force-exe-suffix          Force generation of file with .exe suffix
  --gc-sections               Remove unused sections (on some targets)
  --no-gc-sections            Don't remove unused sections (default)
  --help                      Print option help
  -init SYMBOL                Call SYMBOL at load-time
  -Map FILE                   Write a map file
  --multilib-dir PATH         Specify a target directory
  --no-demangle               Do not demangle symbol names
  --no-keep-memory            Use less memory and more disk I/O
  --no-undefined              Allow no undefined symbols
  --allow-shlib-undefined     Allow undefined symbols in shared objects
  --no-warn-mismatch          Don't warn about mismatched input files
  --no-whole-archive          Turn off --whole-archive
  --noinhibit-exec            Create an output file even if errors occur
  --oformat TARGET            Specify target of output file
  -qmagic                     Ignored for Linux compatibility
  --relax                     Relax branches on certain targets
  --retain-symbols-file FILE  Keep only symbols listed in FILE
  -rpath PATH                 Set runtime shared library search path
  -rpath-link PATH            Set link time shared library search path
  -shared, -Bshareable        Create a shared library
  --sort-common               Sort common symbols by size
  --split-by-file [=SIZE]     Split output sections every SIZE octets
  --split-by-reloc [=COUNT]   Split output sections every COUNT relocs
  --stats                     Print memory usage statistics
  --target-help               Display target specific options
  --task-link SYMBOL          Do task level linking
  --traditional-format        Use same format as native linker
  --section-start SECTION=ADDRESS
                              Set address of named section
  -Tbss ADDRESS               Set address of .bss section
  -Tdata ADDRESS              Set address of .data section
  -Ttext ADDRESS              Set address of .text section
  --verbose                   Output lots of information during link
  --version-script FILE       Read version information script
  --version-exports-section SYMBOL
                              Take export symbols list from .exports, using
                                SYMBOL as the version.
  --warn-common               Warn about duplicate common symbols
  --warn-constructors         Warn if global constructors/destructors are seen
  --warn-multiple-gp          Warn if the multiple GP values are used
  --warn-once                 Warn only once per undefined symbol
  --warn-section-align        Warn if start of section changes due to alignment
  --fatal-warnings            Treat warnings as errors
  --whole-archive             Include all objects from following archives
  --wrap SYMBOL               Use wrapper functions for SYMBOL
  --mpc860c0 [=WORDS]         Modify problematic branches in last WORDS (1-10,
                                default 5) words of a page
/usr/bin/ld: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-l
ittle elf32-big pe-i386 pei-i386 srec symbolsrec tekhex binary ihex trad-core
/usr/bin/ld: supported emulations: elf_i386 i386linux i386pe
/usr/bin/ld: emulation specific options:
elf_i386: 
  -Bgroup               Selects group name lookup rules for DSO
  --disable-new-dtags   Disable new dynamic tags
  --enable-new-dtags    Enable new dynamic tags
  -z initfirst          Mark DSO to be initialized first at runtime
  -z interpose          Mark object to interpose all DSOs but executable
  -z loadfltr           Mark object requiring immediate process
  -z nodefaultlib       Mark object not to use default search paths
  -z nodelete           Mark DSO non-deletable at runtime
  -z nodlopen           Mark DSO not available to dlopen
  -z nodump             Mark DSO not available to dldump
  -z now                Mark object non-lazy runtime binding
  -z origin             Mark object requiring immediate $ORIGIN processing
                          at runtime
  -z KEYWORD            Ignored for Solaris compatibility
i386pe: 
  --base_file <basefile>             Generate a base file for relocatable DLLs
  --dll                              Set image base to the default for DLLs
  --file-alignment <size>            Set file alignment
  --heap <size>                      Set initial size of the heap
  --image-base <address>             Set start address of the executable
  --major-image-version <number>     Set version number of the executable
  --major-os-version <number>        Set minimum required OS version
  --major-subsystem-version <number> Set minimum required OS subsystem version
  --minor-image-version <number>     Set revision number of the executable
  --minor-os-version <number>        Set minimum required OS revision
  --minor-subsystem-version <number> Set minimum required OS subsystem revision
  --section-alignment <size>         Set section alignment
  --stack <size>                     Set size of the initial stack
  --subsystem <name>[:<version>]     Set required OS subsystem [& version]
  --support-old-code                 Support interworking with old code
  --thumb-entry=<symbol>             Set the entry point to be Thumb <symbol>
  --add-stdcall-alias                Export symbols with and without @nn
  --disable-stdcall-fixup            Don't link _sym to _sym@nn
  --enable-stdcall-fixup             Link _sym to _sym@nn without warnings
  --exclude-symbols sym,sym,...      Exclude symbols from automatic export
  --export-all-symbols               Automatically export all globals to DLL
  --kill-at                          Remove @nn from exported symbols
  --out-implib <file>                Generate import library
  --output-def <file>                Generate a .DEF file for the built DLL
  --warn-duplicate-exports           Warn about duplicate exports.
  --compat-implib                    Create backward compatible import libs;
                                       create __imp_<SYMBOL> as well.
  --enable-auto-image-base           Automatically choose image base for DLLs
                                       unless user specifies one
  --disable-auto-image-base          Do not auto-choose image base. (default)
  --dll-search-prefix=<string>       When linking dynamically to a dll witout an
                                       importlib, use <string><basename>.dll 
                                       in preference to lib<basename>.dll 


Supported demangler style: {auto,gnu,lucid,arm,hp,edg,gnu-v3,java,gnat,compaq}

Report bugs to bug-binutils@gnu.org and and hjl@lucon.org


記事内インデックス    連載インデックスはこちら   Interfaceのトップ
出力の種類の制御オプション
C言語の方言を扱うオプション
警告を要求/抑止するオプション
ユーザープログラムまたはGCCをデバッグするためのオプション

Copyright 2002 岸 哲夫

Copyright 1997-2024 CQ Publishing Co.,Ltd.