# Does CMake support linking multiple third party libraries?

**URL:** https://discourse.cmake.org/t/does-cmake-support-linking-multiple-third-party-libraries/4311
**Category:** Usage
**Tags:** os:linux, gen:makefiles
**Created:** [October 19, 2021, 7:23pm UTC](https://discourse.cmake.org/t/does-cmake-support-linking-multiple-third-party-libraries/4311 "2021-10-19T19:23:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![maxcrous](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maxcrous/32/1894_2.png) [@maxcrous](https://discourse.cmake.org/u/maxcrous)
#### Post date: [October 19, 2021, 7:23pm UTC](https://discourse.cmake.org/t/does-cmake-support-linking-multiple-third-party-libraries/4311/1 "2021-10-19T19:23:36Z")

</div>

Hi all! This is a question I have also posted on [Stackoverflow](https://stackoverflow.com/questions/69623544/cmake-how-to-link-multiple-libraries) which has left me rather confused and head scratching.

I am using CMake to define the compilation of a C++ executable. The goal is to use 2 third-party libraries, Open3D and OpenCV. I am able to include one of the two with `target_link_libraries`, but including both results in OpenCV functions not being found.

This is my current `CMakeLists.txt`

```auto
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. By using the commented out Open3D `target_link_libraries`, I can successfully use 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`. The same error even occurs if I include both in a single `target_link_libraries(ORB_SLAM ${OpenCV_LIBS} ${Open3D_LIBRARIES})`. The same error occurs for CMake 3.16.3 and 3.21.3.

The error is as follows:

```auto
/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

```auto
#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;...`. 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?

Here is a dump of all CMake variables if it is of any use [https://textuploader.com/t5dvl/raw](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.

---

<div class="post-metadata">

### Author: ![maxcrous](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maxcrous/32/1894_2.png) [@maxcrous](https://discourse.cmake.org/u/maxcrous)
#### Post date: [October 19, 2021, 9:06pm UTC](https://discourse.cmake.org/t/does-cmake-support-linking-multiple-third-party-libraries/4311/2 "2021-10-19T21:06:45Z")

</div>

Any input, also non-solution, will do.  
Have I misunderstood something fundamental about CMake?  
Is this just an odd unfortunate glitch of something that should normally work?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 19, 2021, 9:59pm UTC](https://discourse.cmake.org/t/does-cmake-support-linking-multiple-third-party-libraries/4311/3 "2021-10-19T21:59:56Z")

</div>

Hmm. `Open3D::Open3D` is probably fine. Can you show what it has in its target properties (somewhere under that `Open3D_DIR` path. Can you show the command lines generated for the compile and link commands (`make VERBOSE=1` or `ninja -v`) in the various situations?

Is it possible that Open3D is finding its own OpenCV as well?

---

<div class="post-metadata">

### Author: ![maxcrous](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maxcrous/32/1894_2.png) [@maxcrous](https://discourse.cmake.org/u/maxcrous)
#### Post date: [October 19, 2021, 10:25pm UTC](https://discourse.cmake.org/t/does-cmake-support-linking-multiple-third-party-libraries/4311/4 "2021-10-19T22:25:09Z")

</div>

Thank you for your quick reply!

Open3D\_DIR contains 4 files,  
Open3DConfig.cmake Open3DTargets.cmake  
Open3DConfigVersion.cmake Open3DTargets-relwithdebinfo.cmake

Which I have uploaded here. [Cmake\_Open3d - Google Drive](https://drive.google.com/drive/folders/1623E8DaU5dTItMbkiXHWuNBzk7IhiaEw?usp=sharing)

I also ran the commands and posted the outputs here:  
make VERBOSE=1: [https://textuploader.com/t511n/raw](https://textuploader.com/t511n/raw)  
ninja --verbose: [https://textuploader.com/t5113/raw](https://textuploader.com/t5113/raw)

Your comment led me to this issue on github, which may be the culprit. [Errors occur when Open3D library is linked together with other libraries, e.g. gflags, opencv · Issue #2286 · isl-org/Open3D · GitHub](https://github.com/isl-org/Open3D/issues/2286)

I will attempt the fixes proposed in that thread and report back. Thanks for the help!
