How to link with OpenCV as cmake subdirectory

asked 2019-08-19 17:39:11 -0600

jyamad gravatar image

I have a project in which I manage most dependencies by tracking them with git submodules and then, where possible, adding them to a CMake build with add_subdirectory. I am having trouble getting this to work with opencv because the include headers seem to be in the wrong spot until OpenCV is actually installed. The targets (e.g. opencv_core, opencv_imgproc) are available to link, but these targets seem not to export the expected build-time include directories.

I get the sense that my approach with git submodules and cmake subdirectories is not the way OpenCV expects this to go. Is there a best practice for linking/installing a custom OpenCV build in the context of a larger project via CMake?

edit retag flag offensive close merge delete

Comments

Hmm, if you build OpenCV without doing a make install the build directory contains all the data you need. You use a submodule to clone the code, ignore the build directory (to avoid git issues) and make sure you link those lib and bin folders correctly. That should do the trick right?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-20 06:30:18 -0600 )edit

I think I have just got it working using a similar scheme, thanks. But it still requires building OpenCV first, which I was initially trying to avoid. Following the newer target-based style in cmake builds, it looks like the opencv targets do pull in their own dependencies, but only after its been built (not at config time). It seems that the includes get collected only at build-time for OpenCV, so the include search path isn't right until the library is fully built. Is that correct?

jyamad gravatar imagejyamad ( 2019-08-20 13:26:46 -0600 )edit

yep that seems correct, and indeed I guess you will need to build OpenCV first before your top project and not during the build. Like in a pre-config phase.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-21 04:47:54 -0600 )edit