添加Sophus库时CMake找不到Sophus的问题

添加Sophus库时CMake找不到Sophus的问题

刚开始使用Sophus时,会遇到这样的错误,

CMake Error at CMakeLists.txt:5 (find_package):
  By not providing "FindSophus.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Sophus", but
  CMake did not find one.
 
  Could not find a package configuration file provided by "Sophus" with any
  of the following names:
 
    SophusConfig.cmake
    sophus-config.cmake
 
  Add the installation prefix of "Sophus" to CMAKE_PREFIX_PATH or set
  "Sophus_DIR" to a directory containing one of the above files.  If "Sophus"
  provides a separate development package or SDK, be sure it has been
  installed.
 
 =
-- Configuring incomplete, errors occurred!

参考这篇博客的思路,https://blog.csdn.net/weixin_38213410/article/details/98114423

进到自己Sophus 源程序文件夹,打开SophusConfig.cmake 文件,文件位置如图1所示。
添加Sophus库时CMake找不到Sophus的问题_第1张图片

打开该文件,看到Sophus_DIR的路径。在cmakelist 文件中设置如下:

cmake_minimum_required(VERSION 3.19)
project(DataStructure)

set(CMAKE_CXX_STANDARD 14)

set(Sophus_DIR "/home/xiujie/SLAM_Lib/Sophus/build")


# 为使用 sophus,您需要使用find_package命令找到它
find_package( Sophus REQUIRED )


include_directories( ${Sophus_INCLUDE_DIRS} )
include_directories("/usr/include/eigen3")

add_executable(DataStructure main.cpp)
target_link_libraries( DataStructure  ${Sophus_LIBRARIES} )

此处set 命令行中的路径即为查找到的路径。

测试代码源自:《视觉SLAM14讲》-ch4:useSophus,直接可用。

main.cpp

#include 
#include 
using namespace std;

#include 
#include 

#include "sophus/so3.h"
#include "sophus/se3.h"

int main( int argc, char** argv )
{
    // 沿Z轴转90度的旋转矩阵
    Eigen::Matrix3d R = Eigen::AngleAxisd(M_PI/2, Eigen::Vector3d(0,0,1)).toRotationMatrix();

    Sophus::SO3 SO3_R(R);               // Sophus::SO(3)可以直接从旋转矩阵构造
    Sophus::SO3 SO3_v( 0, 0, M_PI/2 );  // 亦可从旋转向量构造
    Eigen::Quaterniond q(R);            // 或者四元数
    Sophus::SO3 SO3_q( q );
    // 上述表达方式都是等价的
    // 输出SO(3)时,以so(3)形式输出
    cout<<"SO(3) from matrix: "<

你可能感兴趣的:(工具,c++,slam,linux)