eigen库安装_CMakeLists.txt中常用库文件的查找和链接
时间: 2024-05-01 19:22:07 浏览: 161
当我们使用一些常用的库文件时,可以通过CMakeLists.txt中的find_library和target_link_libraries命令来查找和链接这些库文件。
以Eigen库为例,假设我们已经将Eigen库安装在/usr/local/include下,那么在CMakeLists.txt中可以这样写:
```cmake
cmake_minimum_required(VERSION 3.0)
project(my_project)
# 查找Eigen库
find_library(EIGEN3_LIBS NAMES eigen3 HINTS /usr/local/lib)
# 添加可执行文件
add_executable(my_executable main.cpp)
# 链接Eigen库
target_link_libraries(my_executable ${EIGEN3_LIBS})
```
在上面的代码中,我们使用了find_library命令来查找Eigen库,其中NAMES选项指定了库文件的名称(这里是eigen3),HINTS选项指定了Eigen库文件所在的路径(这里是/usr/local/lib)。
然后,我们使用target_link_libraries命令来将Eigen库链接到我们的可执行文件中。注意,我们使用${EIGEN3_LIBS}来引用查找到的Eigen库文件。
通过这样的方式,我们就可以方便地使用常用的库文件了。
相关问题
使用C++ eigen库翻译以下python代码import pandas as pd import numpy as np import time import random def main(): eigen_list = [] data = [[1,2,4,7,6,3],[3,20,1,2,5,4],[2,0,1,5,8,6],[5,3,3,6,3,2],[6,0,5,2,19,3],[5,2,4,9,6,3]] g_csi_corr = np.cov(data, rowvar=True) #print(g_csi_corr) eigenvalue, featurevector = np.linalg.eigh(g_csi_corr) print("eigenvalue:",eigenvalue) eigen_list.append(max(eigenvalue)) #以下代码验证求解csi阈值 eigen_list.append(1.22) eigen_list.append(-54.21) eigen_list.append(8.44) eigen_list.append(-27.83) eigen_list.append(33.12) #eigen_list.append(40.29) print(eigen_list) eigen_a1 = np.array(eigen_list) num1 = len(eigen_list) eigen_a2 = eigen_a1.reshape((-1, num1)) eigen_a3 = np.std(eigen_a2, axis=0) eigen_a4 = eigen_a3.tolist() k = (0.016 - 0.014) / (max(eigen_a4) - min(eigen_a4)) eigen_a5 = [0.014 + k * (i - min(eigen_a4)) for i in eigen_a4] tri_threshold = np.mean(eigen_a5)
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
std::vector<double> eigen_list;
MatrixXd data(6, 6);
data << 1, 2, 4, 7, 6, 3,
3, 20, 1, 2, 5, 4,
2, 0, 1, 5, 8, 6,
5, 3, 3, 6, 3, 2,
6, 0, 5, 2, 19, 3,
5, 2, 4, 9, 6, 3;
MatrixXd g_csi_corr = data.transpose() * data / 6.0;
EigenSolver<MatrixXd> es(g_csi_corr);
VectorXd eigenvalue = es.eigenvalues().real();
std::cout << "eigenvalue: " << eigenvalue.transpose() << std::endl;
eigen_list.push_back(eigenvalue.maxCoeff());
eigen_list.push_back(1.22);
eigen_list.push_back(-54.21);
eigen_list.push_back(8.44);
eigen_list.push_back(-27.83);
eigen_list.push_back(33.12);
//eigen_list.push_back(40.29);
std::cout << "eigen_list: ";
for (std::vector<double>::iterator it = eigen_list.begin(); it != eigen_list.end(); ++it)
std::cout << *it << " ";
std::cout << std::endl;
int num1 = eigen_list.size();
MatrixXd eigen_a2 = Map<MatrixXd>(eigen_list.data(), num1, 1);
VectorXd eigen_a3 = eigen_a2.array().rowwise().mean().transpose();
VectorXd eigen_a4 = (eigen_a2 - eigen_a3.replicate(num1, 1)).array().abs().rowwise().mean().transpose();
double k = 0.002 / (eigen_a4.maxCoeff() - eigen_a4.minCoeff());
VectorXd eigen_a5 = 0.014 + k * (eigen_a4.array() - eigen_a4.minCoeff());
double tri_threshold = eigen_a5.mean();
std::cout << "tri_threshold: " << tri_threshold << std::endl;
return 0;
}
CMake Error at /opt/ros/melodic/share/catkin/cmake/catkin_package.cmake:183 (message): catkin_package() CATKIN_DEPENDS on 'Eigen3', which has been found in '/usr/lib/cmake/eigen3/Eigen3Config.cmake', but it is not a catkin package Call Stack (most recent call first): /opt/ros/melodic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_package) CMakeLists.txt:111 (catkin_package)
这个错误是由于你的ROS项目依赖于Eigen3库,但是系统中没有找到它的catkin包所导致的。你需要在你的CMakeLists.txt文件中添加以下代码,以确保catkin能够找到Eigen3库:
```
find_package(Eigen3 REQUIRED)
include_directories(${Eigen3_INCLUDE_DIRS})
```
在你的catkin_package()调用中,你需要将'Eigen3'添加到CATKIN_DEPENDS参数中:
```
catkin_package(
CATKIN_DEPENDS roscpp Eigen3
)
```
这样做应该能够解决这个问题。
阅读全文