C++ コンパイラの実行方法/using the c++ compiler =============================================== コンパイラの種類 ---------------- C++コンパイラには、種類がいくつかあります。 目的にあったものを使って下さい。 There are several different C++ compilers. Use the one that suits your prurpose. +------------------------------+--------------------------------------+ || コンパイラの種類/ || 用途/ | || type of compiler || intended purpose | +==============================+======================================+ || 汎用のOSSコンパイラ/ || 言語の勉強や、一般のプログラミング/ | || general purpose || Learning the language and | || open source compilers || general programming | || (g++, clang) | | +------------------------------+--------------------------------------+ || 汎用の商用コンパイラ/ || 同上/ | || general purpose || same as above | || proprietary compilers | | +------------------------------+--------------------------------------+ || 特定ライブラリと共に提供 || そのライブラリを利用するプログラム | || される専用のコンパイラ/ || の作成/ | || compilers provided with || Development of programs using that | || a specific library || library | +------------------------------+--------------------------------------+ Note: - g++ - :term:`GNU C++ Compiler` - clang - the :term:`LLVM` c++ compiler 多くのコンパイラに共通のオプション/Options common to most compilers ------------------------------------------------------------------- コンパイラが違うと、受け付けるオプションが異なりますが、 主要なオプションはある程度デファクト標準化されており、同じ形式を使うことができます。 Compiler options depend on what compiler you are using. However there is a defacto standard for commonly used options. そのような代表的なオプションを表に示します。: The most common options are shown in the table: +----------------+----------------------------------------------------+ | オプション/ | 機能/ | | option | function | +================+====================================================+ |``-c`` || コンパイルのみでリンク省略/ | | || compile only, no linking | +----------------+----------------------------------------------------+ |``-o outfile`` || 出力ファィル名を指定/specify output file | +----------------+----------------------------------------------------+ |``-std=c++11`` || C++の仕様世代を指定/ | | || specify the generation of c++ standards | | || (c++11,c++14,c++17) | +----------------+----------------------------------------------------+ |``-I/abc/def`` || インクルードサーチパスに '/abc/def' を追加する/ | | || Add '/abc/def' to the include search path | +----------------+----------------------------------------------------+ |``-DNAME``, || プリプロセッサマクロを定義する/ | |``-DNAME=VALUE``|| define a preprocessor macro | +----------------+----------------------------------------------------+ |``-lalpha`` || 'alpha' という名のライブラリをリンクする/ | | || link a library named 'alpha' | +----------------+----------------------------------------------------+ |``-L/abc/def`` || ライブラリサーチパスに '/abc/def' を追加する/ | | || Add '/abc/def' to the library search path | +----------------+----------------------------------------------------+ |``-g`` || デバッガ用のシンボル情報を出力に含める/ | | || Output symbol information for the debugger | +----------------+----------------------------------------------------+ |``-O`` ``-O1`` || 最適化を有効にする/ | |``-O2`` || Enable optimization | +----------------+----------------------------------------------------+ |``-Wall`` || 警告を(ほぼ)全てオンにする/ | | || Turn on (mostly) all warning messages | +----------------+----------------------------------------------------+ |``-pedantic`` || コンパイラの標準規格外構文を使ったら警告する/ | | || Warn usages of non-standard syntax | +----------------+----------------------------------------------------+ OSSコンパイラのオプション一覧/Full list of options for some OSS compilers ------------------------------------------------------------------------- 実際のコンパイラは沢山オプションを持っています。有名なものについての リファレンスが乗っているページを挙げておきます。何か問題にぶつかった 時に、必要になるかも知れません。 The actual compilers have many options. Here are the reference pages of some famous compilers. They may become necessary when you run into a problem. - GCC : https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html - Clang : https://clang.llvm.org/docs/ClangCommandLineReference.html よくある使用例/Common usages ---------------------------- コンパイルだけ実施し、リンクはしない/Compile but do not link ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "-c" オプションで実行形式ファイルの作成(ライブラリをオブジェクトファイルに リンクする処理)を抑止することができ、 ソースファイルから :term:`オブジェクトファイル` へのコンパイルだけが 行われるようになります。 The "-c" option will supress creating the executable file (i.e. linking the libraries to the object file), and the compiler will stop at the point when the source file is translated into an :term:`object file`. my_file.cppを翻訳してmy_file.oを出力する: Translate my_file.cpp into my_file.o:: % g++ -c my_file.cpp 出力ファイル名を 'a.out' から変える/ Change the output file name 'a.out' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C++コンパイラでは、実行形式ファイルのデフォルトの名前は 'a.out' です。 "-o" オプションで出力ファイル名を指定することができます。 The default file name for exectuable files is 'a.out'. You can specify the file name with the "-o" option. % g++ -o my_prog my_prog.cpp #リンクまでして、結果をmy_progとする。compile, link, and name it as my_prog C++言語の規格の世代を指定する/specify the generation of the C++ standard ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C++言語の規格は2003年版(C++03)、2011年版(C++11), 2014年版(C++14), 2017年版(C++17), 2020年版(C++20)とあり、どの世代の仕様を使うのかを -stdオプションで 指定します。指定しないとC++03を指定したことになります。 There are several generations for c++ standards: the 2003 version (c++03), and 2011(c++11), 2014(c++14), 2017(c++17), 2020(c++20) versions. You can specify which version to use. If you do not specify any version, c++03 will be assumed. C++14規格を使ってコンパイルする。: Compile using the C++14 standard:: % g++ -std=c++14 -c my_file.cpp インクルードサーチパスを追加する/add an include search path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "-I" オプションで、ヘッダファイルをインクルードする時に探しに行く ディレクトリ名である、インクルードサーチパス(include search path)を 指定できます。: The "-I" option will add an include search path, which is the directory where header files will be searched for:: ../header_files ディレクトリをインクルードサーチパスに加える: Add the directory "../header_files" to the include search path:: % g++ -I../header_files -c my_prog.cpp プリプロセッサマクロを定義する/define a preprocessor macro ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "-Dマクロ名" あるいは"-Dマクロ名=値" で、ソースファイル中に #define を書くの と同様にマクロを定義することができます。 The option of the form "-DMACRO_NAME" or "-DMACRO_NAME=VALUE" will define a macro just as it is written in the source file with a #define . DEBUG_PRINTというマクロを定義してコンパイルする: Compile with the macro DEBUG_PRINT defined:: % g++ -DDEBUG_PRINT -c my_prog.cpp オブジェクトファイルとライブラリをリンクする/link object files and a library ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ コンパラに引数としてオブジェクトファイルを列挙すると、それらがリンク されて実行形式ファイルが作成されます。 If you give object file names as arguments to the compiler, those files will be linked into an executable file. 4つのオブジェクトファイルと "m" ライブラリ(算術関数ライブラリ)をリンクして、 実行形式ファイル "cfd_solver" を作成する: Link 4 object files and the "-m" library (math functions library) and produce an executable file "cfd_solver":: % g++ -o cfd_solver cfd_main.o cfd_element.o cfd_vertex.o cfd_file_io.o -lm オブジェクトファイルをリンクする時点ではコンパイルは済んでいるのでプリプロセッサ は実行されない。そのため "-I" オプションや "-D" オプションは効果を持たない。 Object files are already compiled, so no translation takes place when linking them. Therefore the options "-I" or "-D" will have no effect.