0
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?

はじめに

CMakeは、クロスプラットフォームなビルドシステムを生成するためのツールです。この記事では、CMakeの基本的な使い方を例を交えて書いていきます。

この記事で学べること

  • CMakeの基本構造
  • ライブラリとエグゼキュータブルの分離
  • サブディレクトリの管理
  • ヘッダーファイルのインクルードパス設定

プロジェクト構成

cmake_demo/
├── CMakeLists.txt          # ルートCMakeファイル
├── include/                 # ヘッダーファイル
│   └── math_utils.h
└── src/                     # ソースファイル
    ├── CMakeLists.txt      # srcディレクトリ用CMakeファイル
    ├── main.cpp            # メインプログラム
    └── math_utils.cpp      # ライブラリ実装

ステップ1: プロジェクトの作成

ルートCMakeLists.txt

まず、プロジェクトのルートディレクトリにCMakeLists.txtを作成します。

cmake_minimum_required(VERSION 3.10)
project(MathDemo VERSION 1.0)

# C++14を使用
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# インクルードディレクトリを指定
include_directories(include)

# srcディレクトリを追加
add_subdirectory(src)

ポイント:

  • cmake_minimum_required: 必要なCMakeの最小バージョンを指定
  • project(): プロジェクト名とバージョンを定義
  • set(CMAKE_CXX_STANDARD 14): C++14を使用することを指定
  • include_directories(): ヘッダーファイルの検索パスを追加
  • add_subdirectory(): サブディレクトリのCMakeファイルを読み込む

src/CMakeLists.txt

次に、srcディレクトリ内のCMakeファイルを作成します。

# ライブラリを作成
add_library(math_utils math_utils.cpp)

# 実行ファイルを作成
add_executable(math_demo main.cpp)

# 実行ファイルとライブラリをリンク
target_link_libraries(math_demo math_utils)

ポイント:

  • add_library(): 静的ライブラリを作成
  • add_executable(): 実行ファイルを作成
  • target_link_libraries(): 実行ファイルにライブラリをリンク

ステップ2: ソースコードの作成

include/math_utils.h

include/math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H

namespace MathUtils {
    int add(int a, int b);
    int multiply(int a, int b);
    double divide(double a, double b);
}

#endif // MATH_UTILS_H

src/math_utils.cpp

src/math_utils.cpp
#include "math_utils.h"
#include <stdexcept>

namespace MathUtils {
    int add(int a, int b) {
        return a + b;
    }

    int multiply(int a, int b) {
        return a * b;
    }

    double divide(double a, double b) {
        if (b == 0.0) {
            throw std::runtime_error("Division by zero!");
        }
        return a / b;
    }
}

src/main.cpp

src/main.cpp
#include <iostream>
#include "math_utils.h"

int main() {
    std::cout << "=== CMake Math Demo ===" << std::endl;

    int a = 10, b = 5;

    std::cout << a << " + " << b << " = " << MathUtils::add(a, b) << std::endl;
    std::cout << a << " * " << b << " = " << MathUtils::multiply(a, b) << std::endl;
    std::cout << a << " / " << b << " = " << MathUtils::divide(a, b) << std::endl;

    try {
        MathUtils::divide(10.0, 0.0);
    } catch (const std::runtime_error& e) {
        std::cout << "Error: " << e.what() << std::endl;
    }

    return 0;
}

ステップ3: ビルドと実行

ビルド手順

# ビルドディレクトリを作成
mkdir build
cd build

# CMakeを実行(ビルドシステムを生成)
cmake ..

# ビルド実行
cmake --build .

# または make を使用
make

実行

./src/math_demo

期待される出力

=== CMake Math Demo ===
10 + 5 = 15
10 * 5 = 50
10 / 5 = 2
Error: Division by zero!

CMakeの主要コマンド解説

cmake_minimum_required

cmake_minimum_required(VERSION 3.10)

プロジェクトに必要な最小CMakeバージョンを指定します。

project

project(MathDemo VERSION 1.0)

プロジェクト名とバージョンを定義します。

add_library

add_library(ライブラリ名 ソースファイル...)

静的ライブラリまたは共有ライブラリを作成します。

add_executable

add_executable(実行ファイル名 ソースファイル...)

実行ファイルを作成します。

target_link_libraries

target_link_libraries(ターゲット名 ライブラリ名...)

ターゲットに対してライブラリをリンクします。

include_directories

include_directories(ディレクトリパス...)

ヘッダーファイルの検索パスを追加します。

ビルドタイプの指定

デバッグビルドとリリースビルドを切り替えることができます:

# デバッグビルド
cmake -DCMAKE_BUILD_TYPE=Debug ..

# リリースビルド
cmake -DCMAKE_BUILD_TYPE=Release ..

まとめ

今までは直接Makefileを書いてきたが、自分の好きなビルドツールを使用できるという意味で、CMakeは良いなと感じました。

0
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
0
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?