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?

More than 1 year has passed since last update.

Visual Studio 2022 を用いて CMake プロジェクトから Eigen 3.4.0 を利用

Last updated at Posted at 2023-03-25

Visual Studio 2022 を使用して C++ のプロジェクトを CMake で作成する方法を紹介しています。今回は find_package() によって 線型代数ライブラリ Eigen (https://eigen.tuxfamily.org/) を利用するやり方を報告します。他のライブラリを CMake でビルドするときに Eigen を検出させることができます。

環境

  • 開発環境として、Visual Studio 2022(Community)の「C++デスクトップ環境」がインストールされていること。
  • Git for Windows(https://gitforwindows.org/ )もしくは互換ソフトがインストールされ、コマンドラインから git が使用できること。1
  • CMake (最新版を推奨)がインストールされ、コマンドラインから cmake が実行できること。2
  • 環境変数の設定ができること。3
  • コマンドラインでの作業には「Developer Command Prompt for VS 2022」を使用すること。4

ディレクトリ構成

この記事では、ダウンロードしたソース、コンパイルされたライブラリなどは c:\dev にいれることにします。これは任意の場所に変更することができます。

dev/
├── eigen3
    ├── include
        ├── eigen3
    ├── share
├── make
    ├── eigen-3.4.0
  • ソースコードは c:\dev\make\eigen-3.4.0 にダウンロードします。
  • c:\dev\eigen3 にビルドしたライブラリがインストールされます。
  • インストール後、 c:\dev\make\eigen-3.4.0 は削除できます。

Eigen のインストール

ディレクトリ C:\dev\make がなければ作成し、Developer Command Prompt for VS 2022 で以下を実行する。

> cd \dev\make
> git clone --depth 1 https://gitlab.com/libeigen/eigen -b 3.4.0 eigen-3.4.0
> cd eigen-3.4.0
> md build
> cd build
> cmake -DCMAKE_INSTALL_PREFIX="c:/dev/eigen3" ..
> cmake --build . --target install
  • compiler does not support C++11 というメッセージが出るが、ドキュメントのサンプルコード(の出力予約)が減るだけなので気にしなくて良い。
  • 先に Boost Library (1.53.0 以上)をインストールしておく(別記事)と Boost.Multiprecision が使用可能なことが表示される。
  • Eigen はヘッダオンリーライブラリなので configuration (Release/Debug) の指定は必要ない。
  • CUDA は指定していない。・nvcc with MS Visual Studio does not work https://eigen.tuxfamily.org/dox/TopicCUDA.html

環境変数の設定

次のうちいずれかを行う。上にある設定が優先される。

  • 環境変数 Eigen3_DIRc:\dev\eigen3\share\eigen3\cmake もしくは c:\dev を設定する。
  • 環境変数 CMAKE_PREFIX_PATHc:\dev を設定する。
  • 環境変数 Pathc:\dev\bin を追加する。

ライブラリの使用方法

簡単な例で使い方を説明する。作業用の適当なディレクトリを用意して、以下の内容で test.cppCMakeLists.txt を作成する。

test.cpp
#include <iostream>

#include <Eigen/Dense>
namespace ei = Eigen;
int main()
{
  ei::Matrix3d A{{1,1,1}, {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)

Developer Command Prompt から

> md build
> cd build
> cmake ..

とすることで Visual Studio のプロジェクトである build\test.proj とソリューションである build\test_eigen.sln が作成される。いずれかを Visual Studio で開くことで IDE を用いた開発ができる。あるいは IDE を使わずに、コマンドラインから

> cmake --build . --config Release
> Release\test.exe

としてコンパイル・実行することもできる。

  1. Git from the command line and also from 3rd-party software にチェックを入れてインストールする。

  2. https://cmake.org/ にある Latest Release の Windows x64 Installer を実行して 「Add CMake to the system PATH for all users」にチェックを入れてインストールする。

  3. 環境変数は、設定の「システム」>「バージョン情報」>「システムの詳細設定」から変更できます。> set で環境変数の一覧が見れるので、設定が反映されていない場合には一旦 Windows からサインアウトしてサインインし直してください。

  4. スタートメニューの「Visual Studio 2022」フォルダ内にショートカットがあります。「ターミナル」の設定を変更し、「既定のプロファイル」に「Developer Command Prompt for VS 2022」を登録しておくと、エクスプローラーで作業するディレクトリを開き、コンテキストメニューから「ターミナルで開く」ことができて便利です。

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?