【gcc】オプション備忘録

C言語演習で、<math.h>を使うときは「-lm」オプションをつけたほうがいいと言われてよくわからなかったので、gccのオプションを調べてみた。

結論は、「-lm」オプションをつけると、数学関数を含むオブジェクトライブラリ libm.so または libm.a がリンクされるそうだ。(ヘッダファイルとライブラリは別物ということを初めて知った。)

GNUとは「GNU is Not Unix」の頭文字を取った略号であり、UNIXと上位互換性のあるフリーな総合ソフトウェアシステムの名称。 Richard M. Stallmanを中心とした、世界中の開発者によるボランティア活動によって開発が進められている。

GNU(GNU is Not UNIX) - Linux用語事典 - IT

GNU Compiler Collectionの略。 GCCとは、GNUプロジェクトが開発および配布している、さまざまなプログラミング言語のコンパイラ集のことである。

gccっていったいなんなんだ

-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のオプションも使えた。

Clang は,LLVMのサブプロジェクトである. C言語ファミリ(C,C++,Objective C/C++,OpenCL,CUDA,RenderScript)の機能, GCC互換のコンパイラドライバ (clang) の機能, MSVC互換のコンパイラドライバ (clang-cl.exe) の機能を持つ.

Clang, LLVM, LLD, LLDB のインストール

LLVMプロジェクト
任意のプログラミング言語の静的コンパイルと動的コンパイルの両方をサポートできる最新の SSA ベースのコンパイル戦略を提供することを目的としたプロジェクト。

LLVMに関する解説1

GNUに関する説明の中で、Cコンパイラの定番ともいえるgccについて説明しましたが、実はCコンパイラは、今もものすごい勢いで進化しているのです。世界中に普及したGCCですが、現在それに置き換わるこののできるコンパイラを提供することを目的とした、「Clang(クラン)」というコンパイラの開発プロジェクトが進行しています。

一週間で身につくC言語の基本 コラム19 : LLVMとClang

追記
先人の素晴らしい記事を見つけました。
gccの概要 @keitean

# ~/.zshrc に以下のエイリアスを追加すると、いちいちオプションを打たなくていいから楽です。
alias gcc='gcc -Wall -Wextra -Werror'



この記事が気に入ったらサポートをしてみませんか?