enrico
enrico

Reputation: 67

How can cmake know where the include files and libs are?

I'm successfully compiling a simple DisplayImage project written in C++ and OpenCV using cmake. I followed these instructions: http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html . I really can't get how cmake is able to retrieve OpenCV includes and libs from the CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

I mean shouldn't I tell it where to search for OpenCV? For example /usr/local/lib or /usr/local/include/opencv ...
How is it able to use OpenCV by only telling it I want to use OpenCV and without specifying where to look for it? I think I'm missing something..please help!

Upvotes: 1

Views: 600

Answers (1)

Martin Hierholzer
Martin Hierholzer

Reputation: 930

find_package will run a cmake module (i.e. a script), which will look e.g. in predefined directories for OpenCV. This module will set cmake variables like OpenCV_INCLUDE_DIRS and OpenCV_LIBS. As you are doing correctly, you still have to add those directories and libraries to the appropriate lists in your project, since this depends on what your project is doing: You might be building multiple executables and/or libraries within the same make file but might not want to link all libraries to all executables/libraries.

If you are interested how the module works, you can have a look at it. Usually you will find it in /usr/share/cmake-2.8/Modules (replace "2.8" with the cmake version you are using). I do not have a FindOpenCV.cmake file on my machine, so I guess it is coming with OpenCV. Other Find scripts are coming with cmake itself, so you will have a lot of files already in there. (The directory also has other modules, not only find_package scripts.)

Upvotes: 1

Related Questions