8
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はC++プロジェクトのデファクトスタンダードなビルドシステム。

Makefileを直接書くよりも、CMakeを使ったほうが圧倒的に楽だよ。

最小限のCMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(MyProject)

add_executable(myapp main.cpp)

完全なプロジェクト例

cmake_minimum_required(VERSION 3.16)
project(DemoProject VERSION 1.0.0 LANGUAGES CXX)

# C++17を使用
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# コンパイルオプション
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    add_compile_options(-Wall -Wextra -Wpedantic)
elseif(MSVC)
    add_compile_options(/W4)
endif()

# ビルドタイプの設定
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# ソースファイル
set(SOURCES
    main.cpp
    math_utils.cpp
)

# 実行ファイル
add_executable(demo ${SOURCES})
target_include_directories(demo PRIVATE ${CMAKE_SOURCE_DIR})

ライブラリの作成

# 静的ライブラリ
add_library(math_static STATIC math_utils.cpp)
target_include_directories(math_static PUBLIC ${CMAKE_SOURCE_DIR})

# 共有ライブラリ
add_library(math_shared SHARED math_utils.cpp)
target_include_directories(math_shared PUBLIC ${CMAKE_SOURCE_DIR})

# ヘッダーオンリーライブラリ
add_library(header_only INTERFACE)
target_include_directories(header_only INTERFACE ${CMAKE_SOURCE_DIR}/include)

外部ライブラリの検索

# 標準ライブラリ
find_package(Threads REQUIRED)
target_link_libraries(demo PRIVATE Threads::Threads)

# 任意のライブラリ
find_package(Boost 1.70 COMPONENTS filesystem system)
if(Boost_FOUND)
    target_link_libraries(demo PRIVATE Boost::filesystem Boost::system)
endif()

# pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 REQUIRED sdl2)
target_link_libraries(demo PRIVATE ${SDL2_LIBRARIES})

テストの設定

option(BUILD_TESTS "Build tests" OFF)
if(BUILD_TESTS)
    enable_testing()
    add_executable(test_math test_math.cpp)
    target_link_libraries(test_math PRIVATE math_static)
    add_test(NAME MathTest COMMAND test_math)
endif()

インストール設定

install(TARGETS demo
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)

install(FILES math_utils.h
    DESTINATION include
)

サブディレクトリ構成

project/
├── CMakeLists.txt
├── src/
│   ├── CMakeLists.txt
│   └── main.cpp
├── lib/
│   ├── CMakeLists.txt
│   ├── math_utils.cpp
│   └── math_utils.h
└── tests/
    ├── CMakeLists.txt
    └── test_math.cpp
# トップレベル CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(MultiDir)

add_subdirectory(lib)
add_subdirectory(src)
add_subdirectory(tests)

ビルドコマンド

# 基本的なビルド
mkdir build && cd build
cmake ..
cmake --build .

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

# オプション指定
cmake -DBUILD_TESTS=ON -DBUILD_SHARED_LIBS=ON ..

# 並列ビルド
cmake --build . -j8

実行結果

=== CMake Demo Project ===
add(3, 4) = 7
subtract(10, 3) = 7
multiply(5, 6) = 30
divide(10, 3) = 3.33333
factorial(5) = 120
gcd(24, 36) = 12

CMakeプリセット (CMake 3.19+)

CMakePresets.json:

{
  "version": 3,
  "configurePresets": [
    {
      "name": "debug",
      "binaryDir": "${sourceDir}/build/debug",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug"
      }
    },
    {
      "name": "release",
      "binaryDir": "${sourceDir}/build/release",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release"
      }
    }
  ]
}
cmake --preset debug
cmake --build --preset debug

よく使う変数

変数 説明
CMAKE_SOURCE_DIR ソースのルートディレクトリ
CMAKE_BINARY_DIR ビルドディレクトリ
CMAKE_CURRENT_SOURCE_DIR 現在のCMakeLists.txtのディレクトリ
CMAKE_CXX_COMPILER_ID コンパイラ識別子
PROJECT_VERSION プロジェクトバージョン

まとめ

CMakeの基本を押さえれば、クロスプラットフォームなビルドが簡単に行えます。モダンなCMake(target_*コマンド)を使いましょう!

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