LoginSignup
44
30

More than 5 years have passed since last update.

ターミナルでC++のコンパイル

Posted at

仮にmain.cppというc++ファイルがある場合

$ c++ main.cpp
$ ./a.out


クラスを定義したhファイル(仮にclass.h)、メンバ関数を定義したcppファイル(仮にclass.cpp)、そのクラスを使用したmain.cppの複数ファイルがある場合

$ c++ main.cpp class.cpp
$ ./a.out


別のコンパイル方法

$ c++ -c -o main.o main.cpp
$ c++ -c -o class.o class.cpp
$ c++ main.o class.o
$ ./a.out

この方法では、"-c -o"でそれぞれのオブジェクトファイルを作成するようです。以下のサイトで詳しく解説してありました。
http://www.ysr.net.it-chiba.ac.jp/data/cc.html




今まで数行のプログラムでもXcodeを使ってプログラムを書いていたのですが、ここ数ヶ月Vimでファイルを作成して、ターミナル上からコンパイルするということを今更ながらやっています。主に蟻本の勉強に使っていたので、ファイルは1つですんでいたのですが、複数ファイルをコンパイルしようとして躓きました。Twitterでそのことをツイートしたら、快く教えてくれた人たちがいたので、メモしておきます。

44
30
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
44
30