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

本文介绍了解决Sophus库在CMake配置过程中找不到的问题,并提供了详细的步骤指导。通过设置Sophus_DIR变量和调整CMakeLists.txt文件,可以成功配置Sophus库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加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.youkuaiyun.com/weixin_38213410/article/details/98114423

进到自己Sophus 源程序文件夹,打开SophusConfig.cmake 文件,文件位置如图1所示。
SophusConfig文件位置

打开该文件,看到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 <iostream>
#include <cmath>
using namespace std;

#include <Eigen/Core>
#include <Eigen/Geometry>

#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: "<<SO3_R<<endl;
    cout<<"SO(3) from vector: "<<SO3_v<<endl;
    cout<<"SO(3) from quaternion :"<<SO3_q<<endl;

    // 使用对数映射获得它的李代数
    Eigen::Vector3d so3 = SO3_R.log();
    cout<<"so3 = "<<so3.transpose()<<endl;
    // hat 为向量到反对称矩阵
    cout<<"so3 hat=\n"<<Sophus::SO3::hat(so3)<<endl;
    // 相对的,vee为反对称到向量
    cout<<"so3 hat vee= "<<Sophus::SO3::vee( Sophus::SO3::hat(so3) ).transpose()<<endl; // transpose纯粹是为了输出美观一些

    // 增量扰动模型的更新
    Eigen::Vector3d update_so3(1e-4, 0, 0); //假设更新量为这么多
    Sophus::SO3 SO3_updated = Sophus::SO3::exp(update_so3)*SO3_R;
    cout<<"SO3 updated = "<<SO3_updated<<endl;

    return 0;
}

result

SO(3) from matrix:      0      0 1.5708

SO(3) from vector:      0      0 1.5708

SO(3) from quaternion :     0      0 1.5708

so3 =      0      0 1.5708
so3 hat=
      0 -1.5708       0
 1.5708       0      -0
     -0       0       0
so3 hat vee=      0      0 1.5708
SO3 updated =  7.85398e-05 -7.85398e-05       1.5708


Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值