1.vcpkg安装
先安装vcpkg包管理
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install sophus
vcpkg包管理工具
vcpkg
vcpkg
vcpkg命令
2.
slambook十四讲的ch4中两个cmakelists.txt都要修改
目前编写比较混乱,回头好好看一下cmakelist语法
cmake_minimum_required(VERSION 3.10) # 确保使用3.10或更高版本
# 添加vcpkg工具链文件
set(CMAKE_TOOLCHAIN_FILE "/home/xiehongda/appdata/vcpkg/scripts/buildsystems/vcpkg.cmake")
include(${CMAKE_TOOLCHAIN_FILE})
# 设置vcpkg安装路径
set(CMAKE_PREFIX_PATH "/home/xiehongda/vcpkg/installed/x64-linux" ${CMAKE_PREFIX_PATH})
# 查找并配置Pangolin库
find_package(Pangolin REQUIRED)
include_directories(${Pangolin_INCLUDE_DIRS})
# 查找并配置Sophus库
find_package(Sophus CONFIG REQUIRED)
# 假设Sophus库的头文件位于vcpkg的安装目录中
include_directories(${Sophus_INCLUDE_DIRS})
# 添加可执行文件
add_executable(trajectoryError trajectoryError.cpp)
# 示例:使用关键字签名
target_link_libraries(trajectoryError PUBLIC ${Pangolin_LIBRARIES})
target_link_libraries(trajectoryError PRIVATE Sophus::Sophus)
3.
原本有问题的编写
指令
./vcpkg install sophus
输出
Computing installation plan...
The following packages are already installed:
sophus:x64-linux@1.24.6-r1
sophus:x64-linux is already installed
Total install time: 33.7 us
sophus provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(Sophus CONFIG REQUIRED)
target_link_libraries(main PRIVATE Sophus::Sophus)
指令
./vcpkg integrate install
输出
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=/home/xiehongda/appdata/vcpkg/scripts/buildsystems/vcpkg.cmake"
为了在你的 CMake 项目中使用这些包,你需要确保 CMake 可以找到它们。你可以通过以下步骤来实现这一点:
配置 vcpkg 工具链文件:
在你的 CMakeLists.txt 文件中添加以下内容:
# 设置 vcpkg 的工具链文件路径
set(CMAKE_TOOLCHAIN_FILE "/home/xiehongda/appdata/vcpkg/scripts/buildsystems/vcpkg.cmake")
# 包含工具链文件
include(${CMAKE_TOOLCHAIN_FILE})
查找并链接库:
使用 find_package 命令来查找这些库,并使用 target_link_libraries 将它们链接到你的目标:
# 查找 Eigen3
find_package(Eigen3 REQUIRED)
# 查找 Sophus
find_package(Sophus CONFIG REQUIRED)
# 添加你的可执行文件
add_executable(my_project main.cpp)
# 链接 Eigen3 和 Sophus
target_link_libraries(my_project PRIVATE Eigen3::Eigen Sophus::Sophus)
这样,你的 CMake 项目就能够正确地识别并使用通过 vcpkg 安装的 Eigen3 和 Sophus 库了。
cmake_minimum_required(VERSION 2.8)
project(useSophus)
# 为使用 sophus,需要使用find_package命令找到它
find_package(Sophus REQUIRED)
include_directories(${Sophus_INCLUDE_DIRS})
# Eigen
include_directories("/usr/include/eigen3")
add_executable(useSophus useSophus.cpp)
add_subdirectory(example)
对比原本的cmakelists.txt文件
cmake_minimum_required(VERSION 2.8)
project(useSophus)
# 为使用 sophus,需要使用find_package命令找到它
find_package(Sophus REQUIRED)
include_directories(${Sophus_INCLUDE_DIRS})
# Eigen
include_directories("/usr/include/eigen3")
add_executable(useSophus useSophus.cpp)
add_subdirectory(example)