LoginSignup
1
1

More than 3 years have passed since last update.

g++3分クイックスタート

Last updated at Posted at 2020-05-24

g++を使って以下のc++プログラムを実行可能ファイルへ変換しよう

// hello.cpp file 
#include <iostream> 
int main() 
{ 
    std::cout << "Hello Geek\n"; 
    return 0; 
} 
$ g++ hello.cpp

a.outが生成される。

実行してみる。

$ ./a.out
// Hello Geek

-o オプション

実行可能ファイルに名前を与える。
g++ -o target_name file_name
コンパイルとリンクを行い、実行可能な、ファイルをtarget_nameの名前で生成する。

$ g++ -o main.exe hello.cpp
$ ./main.exe
// Hello Geek

-S オプション

g++ -S file_nameは、アセンブリを生成するのに使用する。

$ g++ -S ./hello.cpp

hello.sというアセンブリが作成される。

-c オプション

オブジェクトファイルを生成するために使用する。

$ g++ -c ./hello.cpp

hello.oというオブジェクトファイルが生成される。

複数ファイルをコンパイル+リンクする。

-cフラグは、ソースコードをオブジェクトコードへと変換する。
-oフラグは、オブジェクトコードを実行可能ファイルへと変換する。

$ g++ -c helloWorld.cpp hello.cpp

.cppファイルをオブジェクトファイルへと変換します。hello.o helloWorld.outが生成される。

$ g++ -o main.exe helloWorld.o hello.o

helloWorld.o hello.oという二つのオブジェクトファイルから実行可能ファイルである main.exeを作成します。

$ ./main.exe
// Hello Geek
// Hello World

これで、c++プログラムから実行可能ファイルを作成出来る。

参考

1
1
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
1
1