LoginSignup
4
10

More than 5 years have passed since last update.

gccのコマンドラインオプションを眺めてて新たに知ったこととかを紹介する(言語非依存編)

Last updated at Posted at 2017-03-30

※続き書きました → gccのコマンドラインオプションを眺めてて新たに知ったこととかを紹介する(C言語・C++編)

こんなことをしようと思った経緯

はじめに

それでは紹介していきます

ファイル名関連

3.2 Options Controlling the Kind of Output

入力ファイル名についての仕様

For any given input file, the file name suffix determines what kind of compilation is done:
(中略)
other
An object file to be fed straight into linking. Any file name with no recognized suffix is treated this way.

各入力ファイルに対し、ファイル名の末尾によって何のコンパイルを行うかが決定される。
(中略)
その他
オブジェクトファイルとみなされ、そのままリンクされる。末尾から何のコンパイルを行うか判別不能なファイル名の場合、このように扱われる。

-x language

Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option.

以降の入力ファイルに対し、言語を(ファイル名の末尾から決定するのではなく)明示的に指定する。このオプションは、次の-xオプションが現れるまで適用される。

(注:例えば、gcc -x c++ foo.cとすると、拡張子はcであるもののC++と扱われる。)

@file

Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed.

コマンドラインオプションをfileから読み込む。そのオプションは、@fileが書かれていた場所に展開される。もしファイルが存在しない場合や読み込めない場合は、そのまま解釈されて取り除かれはしない。

エラー・警告関連

3.8 Options to Request or Suppress Warnings

-Wfatal-errors

This option causes the compiler to abort compilation on the first error occurred rather than trying to keep going and printing further error messages.

エラーが一つ発生したら、(その後も処理を続けて別のエラーを見つけようとする代わりに)そこでコンパイルを止める。

(注:例えば、C++でよくあるエラーメッセージが長い場合に、それを一つずつ表示させ順次バグ取りをしたい場合の利用などが考えられる)

例: https://wandbox.org/permlink/jJipOQg1gGDslN1P
「Run」ボタンでコンパイルをかけることができる。画面左の「Compiler options」に-Wfatal-errorsがあるため、一つ目のint x = "foo";だけしかエラーが表示されない。-Wfatal-errorsを消すと、両方についてエラーが表示される。

デバッグ関連

3.9 Options for Debugging Your Program

-glevel

(中略)
Request debugging information and also use level to specify how much information. The default level is 2.
Level 0 produces no debug information at all. Thus, -g0 negates -g.
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, and line number tables, but no information about local variables.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
(以下略)

デバッグ情報を入れることを指定し、同時にどの程度のデバッグ情報を入れるのかのレベルを指定する。デフォルトは2。

  • 0はデバッグ情報を入れないことを意味する。つまり-g0とすると-gはないものと扱われる。
  • 1は最小限のデバッグ情報を入れる。デバッグするつもりでない箇所については、バックトレースが生成できる程度の情報が入る。関数・外部変数・行番号の情報は入るものの、ローカル変数の情報が入らなくなる。
  • 3はより多くの情報、例えばプログラムにおけるマクロ展開の情報(デバッガによっては対応している)が入る。

プリプロセッサ関連

3.12 Options Controlling the Preprocessor

-Wsystem-headers

Issue warnings for code in system headers. These are normally unhelpful in finding bugs in your own code, therefore suppressed. If you are responsible for the system library, you may want to see them.

システムヘッダに対しても警告を表示する。通常これは、自分のコードに対するバグを見つける上では余計なものになるため、(デフォルトでは)無効化されている。システムライブラリに対して手を入れる場合にはこれが必要になるかもしれない。

-nostdinc

Do not search the standard system directories for header files. Only the directories you have specified with -I options (and the directory of the current file, if appropriate) are searched.

ヘッダファイルの検索において、システム標準のディレクトリを対象としない。-Iオプションで指定したディレクトリ(と、状況によるがカレントディレクトリ)のみが検索対象となる。

-nostdinc++

Do not search for header files in the C++-specific standard directories, but do still search the other standard directories. (This option is used when building the C++ library.)

ヘッダファイルの検索において、システム標準のディレクトリのうちC++特有のものを検索しない。他のシステム標準のディレクトリは検索する。

-isystem dir

Search dir for header files, after all directories specified by -I but before the standard system directories. Mark it as a system directory, so that it gets the same special treatment as is applied to the standard system directories.

dirをヘッダファイルの探索対象ディレクトリとして加える。ただし以下の挙動となる。

  • -Iで指定したディレクトリよりも後、システム標準のディレクトリよりも前に検索される。
  • システムディレクトリとしてマークされる。(上述の通り、標準ではこのディレクトリで見つかったヘッダファイルについては警告が出ない)

-I や -isystem におけるディレクトリ指定

If dir begins with =, then the = will be replaced by the sysroot prefix; see --sysroot and -isysroot.

もしdir(ディレクトリ名)を"="で始めた場合、"="はsysrootの名称(ファイルパスの先頭。"/usr"など。後述の--sysrootオプションも参照)で置き換えられる。

-fdollars-in-identifiers

Accept ‘$’ in identifiers.

"$"を識別子(変数や関数等の名称)として認める。

-C, -CC

-C
Do not discard comments. All comments are passed through to the output file, except for comments in processed directives, which are deleted along with the directive.
You should be prepared for side effects when using -C; it causes the preprocessor to treat comments as tokens in their own right. For example, comments appearing at the start of what would be a directive line have the effect of turning that line into an ordinary source line, since the first token on the line is no longer a ‘#’.

-CC
Do not discard comments, including during macro expansion. This is like -C, except that comments contained within macros are also passed through to the output file where the macro is expanded.
In addition to the side-effects of the -C option, the -CC option causes all C++-style comments inside a macro to be converted to C-style comments. This is to prevent later use of that macro from inadvertently commenting out the remainder of the source line.
The -CC option is generally used to support lint comments.

"-C": (ディレクティブ中を除いて)コメントを除去しないで出力する。これにより起きる副作用として、プリプロセッサにおいてはコメントもトークンとして扱われるというものがある。例えば、コメントがディレクティブの前に存在していると、行の先頭が"#"ではなくなり、その行がディレクティブとは扱われなくなる。
(例)https://wandbox.org/permlink/phi0pYJrkS6Ml7Nr
「Run」ボタンでコンパイルをかけることができる。画面左の「Compiler options」に-Cがあるため、/* COMMENT */#define SLASH 0がマクロ展開と扱われずコンパイルエラーとなる。-Cを取り除くとコンパイルできる。

"-CC": マクロ展開も含めてコメントを除去しない。

リンカ関連

3.14 Options for Linking

-nostdlib

Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored.
(以下略)

リンク時にシステムスタートアップのファイルやライブラリを利用しない。明示的に指定したライブラリのみがリンカに渡される。またシステムライブラリを使うためのオプション(-static-libgcc-shared-libgcc)も無視される。

-s

Remove all symbol table and relocation information from the executable.

実行ファイルからすべてのシンボルないしリロケーション情報を取り除く。(筆者はリロケーションの意義をいまいち理解してない)

ディレクトリ指定関連

3.15 Options for Directory Search

-iquote[dir]

Add the directory [dir] to the head of the list of directories to be searched for header files only for the case of #include "file"; they are not searched for #include , otherwise just like -I.

ディレクトリ[dir]をヘッダファイルの検索対象として加えるが、#include "file"のみ対象とし、#include <file>の際には検索されない。

--sysroot=dir

Use dir as the logical root directory for headers and libraries. For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.

dirをヘッダ・ライブラリのディレクトリの起点とする。例えば、システムが通常はヘッダを/usr/include・ライブラリを/usr/libから検索する場合、これが指定されていると、それぞれ代わりにdir/usr/include・dir/usr/libから検索される。

4
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
10