How to include OpenCV libraries in CMake Makefile

Marcus Barnet picture Marcus Barnet · Mar 31, 2011 · Viewed 13.7k times · Source

I hope you can help me.

I have a simple CMakeLists.txt in order to build my project on Leopard 10.5.8. I'm using CMake 2.8.1 and at the moment this is the code:

cmake_minimum_required(VERSION 2.8)
MESSAGE(STATUS "./src: Going into utils folder")
ADD_SUBDIRECTORY(utils)
MESSAGE(STATUS "./src: utils folder processed")

include_directories(${DIR}/headers)
link_directories (${DIR}/src/utils)

ADD_EXECUTABLE(sample sample.cpp)
TARGET_LINK_LIBRARIES(sample libSample ${EXTERNAL_LIBS})
INSTALL(TARGETS sample DESTINATION "./src")
MESSAGE(STATUS "./src: exiting src folder")

I need to add OpenCV libraries on my project. When I use Eclipse I set the include path to /opt/local/include and the libraries path to: /opt/local/lib and then I specify the libraries name such as_ opencv_core, opencv_imgproc, opencv_video.

Can you tell me how to add these information in the CMakeLists.txt file, please?

I've read some information in the official cmake FAQ but i wasn't able to solve my problem.

Please, help me.

Thanks a lot.

Answer

ltc picture ltc · Apr 1, 2011

You need to add the library names in the TARGET_LINK_LIBRARIES command, but you need to add them without the lib prefix. For example:

include_directories(${DIR}/headers /opt/local/include)
link_directories (${DIR}/src/utils /opt/local/lib)

ADD_EXECUTABLE(sample sample.cpp)
TARGET_LINK_LIBRARIES(sample opencv_core opencv_imgproc opencv_video ${EXTERNAL_LIBS})