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?

More than 1 year has passed since last update.

Eigen3を利用したソースコードをcmakeを用いてビルドする

Last updated at Posted at 2022-06-10

はじめに

  • この文書ではEigen3を利用したc++のソースコードをcmakeを用いてビルドする方法を説明します.
  • EigenのサイトでのCMakeLists.txtの説明と同じですが( Using Eigen in CMake Projects ),英語だと理解に時間がかかるので備忘録的に日本語でこの文書を作成しました.

開発環境

  • 標準的なlinux環境 (Ubuntu18.04.5 LTSを利用)
  • Eigen3 (3.3.4-4を利用)
     - インストール方法sudo apt install libeigen3-dev
  • cmake (3.10.2-1を利用)
     - インストール方法sudo apt install cmake

ビルド手順

  • 開発ディレクトリにEigen3を用いたソースコード (例: sample.cpp) を作成する
  • 開発ディレクトリに以下で説明するCMakeLists.txtを作成する
  • 開発ディレクトリの下にビルドディレクトリ (例: build) を作成する(mkdir build).(ビルドディレクトリの作成は必須ではないが,cmakeの作業ファイルで開発ディレクトリをうるさくしないため)
  • ビルドディレクトリに移動する (cd build)
  • ビルドディレクトリでcmakeを実行する (cmake ..)と,Makefileが生成される
  • makeを実行する (make)
  • ビルドディレクトに実行ファイル (例:sample) が生成される

sample.cpp

sample.cpp
#include <iostream>
#include <Eigen/Core>
int main( ) {
    Eigen::Vector3d v( 0.0, 0.0, 1.0 );
    Eigen::Matrix3d m;
    m << 0.0, -1.0, -1.0, 1.0, 0.0, -2.0, 0.0, 0.0, 1.0;
    std::cout << m * v << std::endl;
    return ( 0 );
}

Eigen3のためのCMakeLists.txt

CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )
project( proj CXX )
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
add_executable( sample sample.cpp )
target_link_libraries( sample Eigen3::Eigen )

実行結果

> ./sample 
-1
-2
 1

CMakeLists.txtの内容の説明

  • 1行目: cmakeの最低バージョンの指定
  • 2行目: プロジェクト名 (proj) と使用言語の指定 (CXX = c++) (省略可能)
  • 3行目: Eigen3の利用を宣言 (主にinclude pathの設定)
  • 4行目: 実行形式名(sample)とそのソースコード (sample.cpp) の指定
  • 5行目: 実行形式に対する追加のリンクライブラリを指定 (Eigen::Eigen)

おわりに

  • Eigenを利用したソースコードのビルドはインクルードパスの指定が面倒ですよね.
  • cmakeを導入すると,Eigenを利用するときの典型的なCMakeLists.txtをテンプレートして利用することで,簡単にビルド (make) ができるようになります.
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?