导入在 mac 上使用 vcpkg 安装的 C++ 库?

Importing C++ library installed using vcpkg on mac?

我的目标是在我的 Mac 上安装一个库 (rbdl-orb) 并在 C++ 程序中导入该库。

我的第一次尝试是直接克隆库和 运行 包含的示例。程序“example.cc”以以下两行开头:

#include <iostream>
#include <rbdl/rbdl.h>

CMake文件如下:

PROJECT (RBDLEXAMPLE CXX)

CMAKE_MINIMUM_REQUIRED(VERSION 3.0)

# We need to add the project source path to the CMake module path so that
# the FindRBDL.cmake script can be found.
LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR} )


SET(CUSTOM_RBDL_PATH "" CACHE PATH "Path to specific RBDL Installation")
# Search for the RBDL include directory and library
FIND_PACKAGE (RBDL REQUIRED)
FIND_PACKAGE (Eigen3 3.0.0 REQUIRED)

# Add the include directory to the include paths
INCLUDE_DIRECTORIES ( ${RBDL_INCLUDE_DIR} ${EIGEN3_INCLUDE_DIR} )

# Create an executable
ADD_EXECUTABLE (example example.cc)

# And link the library against the executable
TARGET_LINK_LIBRARIES (example
    ${RBDL_LIBRARY}
    )

键入“make example”会产生以下错误:

c++     example.cc   -o example
example.cc:10:10: fatal error: 'rbdl/rbdl.h' file not found
#include <rbdl/rbdl.h>
         ^~~~~~~~~~~~~
1 error generated.
make: *** [example] Error 1

我猜想安装库需要一些步骤,这样 C++ 编译器在看到命令“import”时就知道去哪里寻找文件。 The library's GitHub 注意到该软件包可通过 vcpkg 安装。

我按照instructions安装了vcpkg。然后,我通过在终端中键入“vcpkg install rbdl”来构建库。在工作目录中键入“vcpkg list”显示库似乎已安装:

(base) my_name@my_name-MacBook-Pro current_directory % vcpkg list
eigen3:arm64-osx                                   3.4.0#2          C++ template library for linear algebra: matrice...
rbdl:arm64-osx                                     2.6.0#2          Rigid Body Dynamics Library
vcpkg-cmake-config:arm64-osx                       2022-02-06       
vcpkg-cmake:arm64-osx                              2022-04-07

不幸的是,再次输入“make example”会产生以下错误:

c++     example.cc   -o example
example.cc:10:10: fatal error: 'rbdl/rbdl.h' file not found
#include <rbdl/rbdl.h>
         ^~~~~~~~~~~~~
1 error generated.
make: *** [example] Error 1

该错误表明 C++ 编译器不知道库的安装位置。

如何正确安装库并将其导入我的 C++ 程序?

我不确定这是最好的方法,我假设 mac 和 linux 在这里很相似...

我想您可以搜索 rgbd.cmake 的位置并将其附加到 CMAKE_MODULE_PATH 变量。为了进行非常快速的搜索,我以这种方式使用了 ripgrep :

$ rg --files / 2>/dev/null|rg rgbd.*\.cmake$
... expect to chose a path from the outputed ones.

如果您有输出,请告诉我。如果是,则将其添加到 cmakelist:

list(APPEND CMAKE_MODULE_PATH "the path to rgbd.cmake")

希望对您有所帮助。

使用“sudo”重新安装库并开始使用 vcpkg 安装并对 CMake 文件进行以下更改:

设置(CMAKE_CXX_STANDARD11)

经过这些更改后,库 运行 正确。