2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C++】Makefile を使って、コンパイルコマンドを簡単にする

Posted at

いつものコンパイル

競プロとかの場面。
cppファイルを作成し、こんなコマンドでコンパイルするが…
…ぶっちゃけ面倒くさい。

clang++ -std=c++23 -O2 source.cpp -o output

それで、Makefileなるものがあることを知って、使ってみました。
macOSです。

Makefile を使ったコンパイル

適当なプログラムを作ります。ファイル名は、適当に source.cpp とします。

source.cpp
#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;

    return 0;
}

Makefileという名前のファイルを作成します。名前は一言一句同じにします。

workspace
  |- source.cpp
+ |- Makefile  // これを作成する

Makefile の中身

これまで打っていたコマンドをコピペして、こんな感じにします。
インデントは、スペースではなくタブを使います!

Makefile
# ソースファイル「source.cpp」
#  出力ファイル「output」
output: source.cpp
	clang++ -std=c++23 -O2 source.cpp -o output

実行してみる

以下コマンドでコンパイル。
シンプルでいいね!

make

実行してみると、正しく動きました。

./output
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?