# Importing OpenCV, versions conflicts

**URL:** https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343
**Category:** Usage
**Created:** [May 17, 2021, 9:49am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343 "2021-05-17T09:49:56Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 17, 2021, 9:49am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/1 "2021-05-17T09:49:56Z")

</div>

Hi,

I fail to properly add a dependency to opencv3 in my devs.

The setup is the following:  
I’ve got two version of opencv:  
2.4 which is the default one for ubuntu 16.04  
3.3 which is provided inside ROS kinetic

I’ve got a static library L that depends publicly on OpenCV  
I’ve got an application A that depends privately on L and OpenCV

find\_package(OpenCV REQUIRED) finds the 3.3 version, which is what I expect.  
I require all my dependencies to use namespace for homogeneity but OpenCV does not provide OpenCV::OpenCV.

On CMake 3.19 I do this:  
find\_package(OpenCV REQUIRED)  
add\_library(OpenCV::OpenCV UNKNOWN IMPORTED)  
set\_target\_properties(OpenCV::OpenCV PROPERTIES  
IMPORTED\_LINK\_INTERFACE\_LANGUAGES “CXX”  
IMPORTED\_LOCATION “${OpenCV\_LIBRARIES}”  
INTERFACE\_INCLUDE\_DIRECTORIES “${OpenCV\_INCLUDE\_DIRS}”  
)  
target\_link\_libraries(${TARGET\_NAME} ${Inheritance} OpenCV::OpenCV)

On CMake 3.5 (required by some customers…) I do this:  
target\_include\_directories(${TARGET\_NAME} ${Inheritance} ${OpenCV\_INCLUDE\_DIRS})  
target\_link\_libraries(${TARGET\_NAME} ${Inheritance} ${OpenCV\_LIBRARIES})

${Inheritance} is PUBLIC for L  
${Inheritance} is PRIVATE for A

With CMake 3.5 my codes get built and can be executed.  
But on CMake 3.19 I’ve got a build error, telling me that there is no rule to build target opencv\_calib3d required for my target (actually opencv\_calib3d is the first lib in OpenCV\_LIBRARIES)

Besides I do it twice:  
first in a static library L that publicly depends on OpenCV  
then on my application A that depends both on L and on OpenCV

then in the build.make file of A, I see the list of OpenCV libraries twice. The build issues then a warning about an overloaded rule.

What do I miss?

NB1 it seems that all libraries names in ROS/OpenCV are finished by “3”, for instance opencv\_calib3d3.so  
NB2 I can’t locate the .a files within ROS/OpenCV  
NB3 using  
target\_include\_directories(${TARGET\_NAME} ${Inheritance} ${OpenCV\_INCLUDE\_DIRS})  
target\_link\_libraries(${TARGET\_NAME} ${Inheritance} ${OpenCV\_LIBRARIES})  
with CMake 3.19 works so it seems to be really an issue with the way I create OpenCV::OpenCV  
Which puzzles me is that in both cases (with or without OpenCV::OpenCV wrapping) all environment and cmake variables are the same…

Many thanks in advance

---

<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: [May 17, 2021, 1:48pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/2 "2021-05-17T13:48:56Z")

</div>

If you depend on an imported target from an installed package (basically grep the generated target information in the generated CMake package files for the imported target name), you need to provide that target as well. So things are probably failing when L mentions `OpenCV::OpenCV` in its interface and then A needs that same target. So basically, L might need to `find_dependency(OpenCV)` and then regenerate the same imported target again from `find_package(L)`.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 17, 2021, 3:23pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/3 "2021-05-17T15:23:05Z")

</div>

Hi

This is the LConfig.cmake  
include(CMakeFindDependencyMacro)  
foreach(_lib_ IN ITEMS OpenCV;OtherDependencies)  
find\_dependency(${_lib_} REQUIRED)  
endforeach()

# include the generated target file  
include("${CMAKE\_CURRENT\_LIST\_DIR}/LTargets.cmake")

L and A use find\_package(OpenCV REQUIRED) and wrap it into OpenCV::OpenCV as described above

is it not the proper way to go?  
(by the way in this thread [proper way to wrap an imported target around a third-party target - #8 by adaldev](https://discourse.cmake.org/t/proper-way-to-wrap-an-imported-target-around-a-third-party-target/2757/8), I described several generic use cases for which I failed to find a clear and correct procedure).

thanks

---

<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: [May 17, 2021, 9:44pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/4 "2021-05-17T21:44:56Z")

</div>

If `L` makes `OpenCV::OpenCV` in its build, it should make it in the package too. I would recommend naming it `L::opencv` or something though to avoid conflicting with any `OpenCV::` targets which may exist in the future.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 18, 2021, 7:59am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/5 "2021-05-18T07:59:37Z")

</div>

Hi,

Sorry, I will then fall back to the thread mentioned above.  
Indeed I feel that I don’t understand the way to package a third-party on the fly and to propagate it through dependencies.  
I took inspiration from Daniel Pfeiffer presentation but it seems that I got it wrong.  
I don’t understand what’s to be done on the package.  
I also read several times the official CMake doc: [https://cmake.org/cmake/help/git-stage/guide/importing-exporting/index.html](https://cmake.org/cmake/help/git-stage/guide/importing-exporting/index.html)  
but, honestly, I could not understand it, especially in the context of third party targets.

I hope that my questions in the other thread may have an answer. I think that it may help a bunch of other peoples to that may otherwise use “workarounds” 😉.

Regards  
A.

---

<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: [May 18, 2021, 6:29pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/6 "2021-05-18T18:29:45Z")

</div>

I just leave the imported targets to the packages or custom find modules. Doing it outside of the `find_package` call is a mess. I recommend that you contribute an `IMPORTED` target to whatever `find_package(OpenCV)` is calling.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 21, 2021, 4:56pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/7 "2021-05-21T16:56:59Z")

</div>

Hello

I tried the FindOpenCV.cmake approach (see joined file)  
[FindOpenCV.cmake](https://discourse.cmake.org/uploads/short-url/mYDJdnZ4bnpGPo7gVm8QkkB3aE.cmake) (671 Bytes)

L compiles and install  
When configuring A, OpenCV is find twice and the second time, all its libraries are listed twice.  
On the whole, the OpenCV modules are listed three times…  
When building I’ve still got the error “missing rule to build the target opencv\_calib3d”…

I can’t see where is the error as the joined file seems pretty close to examples found on the web.

Regards.

---

<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: [May 24, 2021, 12:55pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/8 "2021-05-24T12:55:16Z")

</div>

Could you please provide a stripped down, full example of what’s going on here? Speaking in abstract terms makes it hard to make recommendations on what else to do.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 25, 2021, 10:05am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/9 "2021-05-25T10:05:43Z")

</div>

Ouch,

It took me some work to provide a small example.

It still produces the missing rule error and a warning about rule overload.  
But it does not reproduce the multiple finding of OpenCV. I will look into that to find what may be different between my real code and the small example.

Nevertheless, it might be already useful to find why I don’t properly create OpenCV::OpenCV

Regards  
[CMakeDemo.7z](https://discourse.cmake.org/uploads/short-url/wKXvebpHE6tmqL4J9Vh2MPpLMp2.7z) (2.3 KB)  
PS I provide a small Run.sh script to easily reproduce the error  
PPS As a reminder: I’ve got several OpenCV version on my computer. The one found with find\_package and NO\_MODULE is the one wrapped into ROS

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [June 1, 2021, 8:08am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/10 "2021-06-01T08:08:15Z")

</div>

Hello,

Would it be possible to have a feedback on the above example?

Regards

---

<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: [June 1, 2021, 8:30pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/11 "2021-06-01T20:30:42Z")

</div>

I took a look. It seems that the issue is (word wrapped for ease of reading):

```cmake
/builds/opencv/CMakeDemo/MyPackages/FindOpenCV.cmake(21):
set_target_properties(OpenCV::OpenCV
  PROPERTIES
    IMPORTED_LINK_INTERFACE_LANGUAGES CXX
    IMPORTED_LOCATION opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_alphamat;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_cvv;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_mcc;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_rapid;opencv_reg;opencv_rgbd;opencv_saliency;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_wechat_qrcode;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
    INTERFACE_INCLUDE_DIRECTORIES /usr/include/opencv4)

```

`IMPORTED_LOCATION` is the _full path_ to the library that is being represented. I think you want `INTERFACE_LINK_LIBRARIES` instead here.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [June 3, 2021, 8:57am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/12 "2021-06-03T08:57:06Z")

</div>

Hi,

replacing IMPORTED\_LOCATION with INTERFACE\_LINK\_LIBRARIES gives on Ubuntu with CMake 3.19.2:  
CMake Error in CMakeLists.txt:  
IMPORTED\_LOCATION not set for imported target “OpenCV::OpenCV”  
(both for L and A)

on windows with CMake 3.17.0 L compiles but A gives:  
CMakeFiles\A.dir\build.make(92) : fatal error U1033: syntax error : unexpected ‘::’

Another difference between my windows and my ubuntu machine is that, on windows, I compiled OpenCV myself (a msvc version and a mingw one), while on Ubuntu 16.04 I’ve got the default one plus the one shipped with ROS kinetic (which is the one I locate with find\_package).

I don’t understand much of the documentation except that I must avoid to provide absolute path to my package if I want to make them relocatable

regards

---

<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: [June 3, 2021, 1:54pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/13 "2021-06-03T13:54:37Z")

</div>

Make it an `INTERFACE` library rather than `UNKNOWN`.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [June 3, 2021, 3:18pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/14 "2021-06-03T15:18:24Z")

</div>

Thx,

To be confirmed, it seems to work on the Linux side.

Still not working on windows/MSVC/CMake 3.17  
when calling cmake on L:  
INTERFACE\_LIBRARY targets may only have whitelisted properties. The  
property “IMPORTED\_LINK\_INTERFACE\_LANGUAGES” is not allowed.

---

<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: [June 3, 2021, 3:49pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/15 "2021-06-03T15:49:42Z")

</div>

Strange that is platform specific, but that properly can be removed. The OpenCV imported targets should already have the link language set.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [June 4, 2021, 9:38am UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/16 "2021-06-04T09:38:56Z")

</div>

Hi,

Now it works! Thanks.  
A bit frustrating though because I don’t understand why (why importing as `INTERFACE`, why removing the language (the doc tells that it should be ignored at worse, not generating an error), why populating the lib with `INTERFACE_LINK_LIBRARIES`?..).

regards  
A.

---

<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: [June 4, 2021, 12:45pm UTC](https://discourse.cmake.org/t/importing-opencv-versions-conflicts/3343/17 "2021-06-04T12:45:20Z")

</div>

> [@adaldev](#):
>
> why importing as `INTERFACE`

Because the target represents multiple libraries, it has to be. Non-interface targets need to be a _single_ library import.

> [@adaldev](#):
>
> why removing the language (the doc tells that it should be ignored at worse, not generating an error)

This sounds like a bug

> [@adaldev](#):
>
> why populating the lib with `INTERFACE_LINK_LIBRARIES`?..).

Because you’re saying “if you use `OpenCV::OpenCV`, you need these libraries”. That’s what the `INTERFACE_LINK_LIBRARIES` target is for.
