[转]Clion中如何使用矩阵库eigen
CMakelist文件内容如下:
cmake_minimum_required(VERSION 3.2) project(PISO) set(CMAKE_CXX_STANDARD 14) include_directories(C:/Users/DELL/Downloads/eigen-3.3.8) add_executable(${PROJECT_NAME} main.cpp)
其中第一行为确定cmake的最低版本,第二行为项目名称,第三行是确定Cpp的标准,第五行为添加执行文件,eigen库的调用关键在于第四行:
include_directories(C:/Users/DELL/Downloads/eigen-3.3.8)
我们只需要下载eigen库,然后括号内写入库的文件位置(这里我直接放在下载路径下),即可直接调用。提供如下测试代码:
#include <iostream> #include <Eigen/Dense> using namespace Eigen; using namespace std; int main() { MatrixXd m = MatrixXd::Random(3,3); m = (m + MatrixXd::Constant(3,3,1.2)) * 50; cout << "m =" << endl << m << endl; VectorXd v(3); v << 1, 2, 3; cout << "m * v =" << endl << m * v << endl; }
上述代码运算结果为:
C:\Users\DELL\CLionProjects\untitled3\cmake-build-debug\PISO.exe m = 10.1251 90.8741 45.0291 66.3585 68.5009 99.5962 29.3304 57.9873 92.284 m * v = 326.961 502.149 422.157 Process finished with exit code 0
当然,如果在此基础上又在CMakeLists.txt文件中添加了find_package(Eigen3 3.2.6 REQUIRED)代码,则同样需要在CMakeLists.txt文件中添加set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})代码,才能正确配置Eigen库。完整代码如下:
cmake_minimum_required(VERSION 3.24) project(CLionEigen) set(CMAKE_CXX_STANDARD 11) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) SET(EIGEN3_INCLUDE_DIR "C:\\3rdParty\\eigen-3.2.6\\eigen-3.2.6") IF( NOT EIGEN3_INCLUDE_DIR ) MESSAGE( FATAL_ERROR "Please point the environment variable EIGEN3_INCLUDE_DIR to the include directory of your Eigen3 installation.") ENDIF() INCLUDE_DIRECTORIES ( "${EIGEN3_INCLUDE_DIR}" ) # Eigen3 find_package(Eigen3 3.2.6 REQUIRED) add_executable(CLionEigen main.cpp)#CLionEigen为项目名称
原文链接:https://blog.csdn.net/ambu1230/article/details/128444728