简体   繁体   English

Cmake:如何链接多个库?

[英]Cmake: How to link multiple libraries?

I am using CMake to define the compilation of a C++ executable.我正在使用 CMake 来定义 C++ 可执行文件的编译。 The goal is to use 2 third-party libraries, Open3D and OpenCV.目标是使用 2 个第三方库,Open3D 和 OpenCV。 I am able to include one of the two with target_link_libraries , but including both results in OpenCV functions not being found.我可以将两者之一包含在target_link_libraries ,但在未找到 OpenCV 函数中包含这两个结果。

This is my current CMakeLists.txt这是我当前的CMakeLists.txt

minimum_required(VERSION 3.20)
project(ORB_SLAM)

find_package(Open3D REQUIRED)
find_package(OpenCV REQUIRED)

set(CMAKE_CXX_STANDARD 20)
add_executable(ORB_SLAM src/main.cpp)

#target_link_libraries(ORB_SLAM ${Open3D_LIBRARIES})
target_link_libraries(ORB_SLAM ${OpenCV_LIBS})

# When printed, ${Open3D_LIBRARIES} = Open3D::Open3D
#               ${OpenCV_LIBS} = opencv_calib3d;opencv_core;...many more..;opencv_xphoto

With this CMakeList.txt , I can successfully use OpenCV functions.有了这个CMakeList.txt ,我可以成功地使用 OpenCV 函数。 By using the commented out Open3D target_link_libraries , I can successfully use Open3D.通过使用注释掉Open3D target_link_libraries ,我可以成功地使用Open3D。 When uncommenting both target_link_libraries , it fails to find OpenCV functionality, regardless of the order of the find_package and target_link_libraries .当在取消两个target_link_libraries ,它未能找到OpenCV的功能,无论是顺序的find_packagetarget_link_libraries The same error even occurs if I include both in a single target_link_libraries(ORB_SLAM ${OpenCV_LIBS} ${Open3D_LIBRARIES}) .如果我将两者都包含在一个target_link_libraries(ORB_SLAM ${OpenCV_LIBS} ${Open3D_LIBRARIES})甚至会发生同样的错误。 The same error occurs for CMake 3.16.3 and 3.21.3. CMake 3.16.3 和 3.21.3 也会出现同样的错误。

The error is as follows:错误如下:

/usr/bin/ld: CMakeFiles/ORB_SLAM.dir/src/main.cpp.o: in function `main':
/home/m/CLionProjects/ORB_SLAM/src/main.cpp:20: undefined reference to `cv::VideoCapture::VideoCapture(std::string const&, int)'

For the code对于代码

#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
//#include <open3d/Open3D.h>

int main() {
    cv::VideoCapture cap("/home/.../scene.mp4");
    //auto sphere = open3d::geometry::TriangleMesh::CreateSphere(1.0);  
}

It seems as though Open3D::Open3D takes precedence over opencv_calib3d;opencv_core;... .似乎Open3D::Open3D优先于opencv_calib3d;opencv_core;... What is causing this and how can I fix it?这是什么原因造成的,我该如何解决? Is this perhaps due to the discrepancy in Open3D's "::" vs OpenCV's lowercase notation?这可能是由于 Open3D 的“::”与 OpenCV 的小写符号存在差异?

Edit: Here is a dump of all CMake variables if it is of any use https://textuploader.com/t5dvl/raw编辑:这是所有 CMake 变量的转储,如果它有任何用途的话https://textuploader.com/t5dvl/raw

Excuse my inexperience.原谅我的经验不足。 I have searched through CMake documentation and Stackoverflow questions for a lead, but so far I have found nothing.我已经搜索了 CMake 文档和 Stackoverflow 问题以寻找线索,但到目前为止我一无所获。

The problem was solved by finding this Github issue: https://github.com/isl-org/Open3D/issues/2286通过找到这个 Github 问题解决了这个问题: https : //github.com/isl-org/Open3D/issues/2286
By using specific build flags when building Open3D, the libraries could both be linked correctly and simultaneously with the target_link_libraries(ORB_SLAM ${OpenCV_LIBS} ${Open3D_LIBRARIES}) command.通过在构建 Open3D 时使用特定的构建标志,可以使用target_link_libraries(ORB_SLAM ${OpenCV_LIBS} ${Open3D_LIBRARIES})命令正确并同时链接库。

The build commands were as follows;构建命令如下;

git clone --recursive https://github.com/intel-isl/Open3D
cd Open3D && source util/scripts/install-deps-ubuntu.sh
mkdir build && cd build
cmake -DBUILD_EIGEN3=ON -DBUILD_GLEW=ON -DBUILD_GLFW=ON -DBUILD_JSONCPP=ON -DBUILD_PNG=ON -DGLIBCXX_USE_CXX11_ABI=ON -DPYTHON_EXECUTABLE=/usr/bin/python -DBUILD_UNIT_TESTS=ON ..
make -j4
sudo make install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM