【gcc】オプション備忘録
C言語演習で、<math.h>を使うときは「-lm」オプションをつけたほうがいいと言われてよくわからなかったので、gccのオプションを調べてみた。
結論は、「-lm」オプションをつけると、数学関数を含むオブジェクトライブラリ libm.so または libm.a がリンクされるそうだ。(ヘッダファイルとライブラリは別物ということを初めて知った。)
-o:
Place the primary output in file "file". This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
If -o is not specified, the default is to put an executable file in "a.out", the object file for "source.suffix" in "source.o", its assembler file in "source.s", a precompiled header file in "source.suffix.gch", and all preprocessed C source on standard output.
(※[.suffix]は拡張子のことを指すらしい。)
-c:
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
By default, the object file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, ‘.s’, etc., with ‘.o’.
Unrecognized input files, not requiring compilation or assembly, are ignored.
(Reference: https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html )
-l library:
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.
The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.
Static libraries are archives of object files, and have file names like lib library.a. Some targets also support shared libraries, which typically have names like lib library.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
(Reference: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html )
-Werror:
Make all warnings into errors.
-Wall:
This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.
-Wextra:
This enables some extra warning flags that are not enabled by -Wall. (This option used to be called -W. The older name is still supported, but the newer name is more descriptive.)
(Reference:https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html )
$ man gcc
No manual entry for gcc
だったから、gccにmanは無いと思ったけど、
$ man clang
は実行できた。ちなみに、gccのオプションも使えた。
追記
先人の素晴らしい記事を見つけました。
gccの概要 @keitean
# ~/.zshrc に以下のエイリアスを追加すると、いちいちオプションを打たなくていいから楽です。
alias gcc='gcc -Wall -Wextra -Werror'
この記事が気に入ったらサポートをしてみませんか?