Vyacheslav Vyacheslav - 1 year ago 206
C++ Question

linking opencv libraries included as an external project via cmake

I'm relatively new to cmake and after days of struggling couldn't figure out the following thing:

I have a project that depends on opencv (which is a cmake project on its own) and I want to statically link opencv libraries. What I'm doing is I have a copy of opencv source code in my project and include it into my CMakeLists.txt via


All built nicely and where I have problem is that I cannot reliably determine where the opencv libraries will be. E.g. on linux/mac there are in
and are named as
whereas on 32-bit Windows with VS 2012 installed the libs are in
and for the Debug configuration the libs named like

So the question is can I somehow obtain a list of all libraries produced by the OpenCV build (and the root lib folder) and link them via something like
target_link_libraries(mylib opencv_core ...)

Maybe I'm doing something wrong or overcomplicated. So what I basically want is to compile my embedded opencv source tree statically and link against its libraries in a "cross-plaformish" way.

Any pointers are highly appreciated! Thanks!

Answer Source

The best solution to use cmake with OpenCV project is:

  1. Compile OpenCV as shared / static libraries with OpenCV's cmake build system.
  2. In your project's CMakeLists.txt

For example (CMakeLists.txt):

project(test_proj C CXX)

  set(OpenCV_DIR "d:/libs/opencv-2.4.8/build")
  set(OpenCV_DIR "/usr/lib/opencv")
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui)

add_executable(test main.cpp)
target_link_libraries(test ${OpenCV_LIBS})

It works in cross-platforms.