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のインストールとCMakeLists.txtの書き方まとめ

Posted at

cmakeのインストールと簡単な例の実行

cmakeのインストール

apt install

$ apt install cmake

ソースからビルド

apt installを用いたインストールではうまくいかない場合、下記を実行することでソースからビルドすることができます。

$ wget https://github.com/Kitware/CMake/releases/download/v3.23.3/cmake-3.23.3.tar.gz
$ tar -zxvf cmake-3.23.3.tar.gz
$ cd cmake-3.23.3
$ sudo ./bootstrap
$ sudo make
$ sudo make install
$ hash -r
$ cmake --version

簡単な例の実行

当項ではEigenライブラリを用いた簡単な実行例について確認します。

test.cpp
#include <iostream>

#include <Eigen/Dense>

int main()
{
  Eigen::Matrix3d A{{1,1,2}, {0,1,1}, {0,0,1}};
  std::cout << A << std::endl;
  std::cout << A*A << std::endl;

  return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(test_eigen CXX)
set(CMAKE_CXX_STANDARD 14)

find_package(Eigen3 REQUIRED)
message(STATUS "Found Eigen-" ${EIGEN3_VERSION_STRING})

add_executable(test test.cpp)
target_link_libraries(test Eigen3::Eigen)

上記のようにtest.cppCMakeLists.txtを用意し、下記を実行することでビルド(実行ファイルの作成)と実行を行うことができます。

$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./test

・実行結果

1 1 2
0 1 1
0 0 1
1 2 5
0 1 2
0 0 1

CMakeLists.txtの書き方

コマンド名 概要
cmake_minimum_required 最低限必要なcmakeのバージョン
project プロジェクト名と使用するプログラム言語の指定
set(x, y) 変数xに値yを代入
find_package モジュールの確認
message 標準出力(Pythonprint文などと同様)
add_executable(x, y.cpp) y.cppから実行ファイルxを作成
target_link_libraries
add_subdirectory サブディレクトリを追加(各ディレクトリ毎にCMakeLists.txtを作成)
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?