46

I consider this a fundamental step for creating projects that use OpenCV libraries so you don't need to manually include all the libraries. There is not detailed information on this topic, at least for a newbie that just wants to use OpenCV as soon as posible, so:

Which is the easiest and scalable way to create a multiplatform c++ OpenCV with Cmake?

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164

2 Answers2

37

First: create a folder Project containing two subfolders src and include, and a file called CMakeLists.txt.

Second: Put your cpp inside the src folder and your headers in the include folders.

Third: Your CMakeLists.txt should look like this:

cmake_minimum_required(VERSION 2.8) 
PROJECT (name)
find_package(OpenCV REQUIRED )
set( NAME_SRC
    src/main.cpp    
)

set( NAME_HEADERS       
     include/header.h
)

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( name ${NAME_SRC} ${NAME_HEADERS} )

target_link_libraries( sample_pcTest ${OpenCV_LIBS} )

Fourth: Open CMake GUI and select the root folder as input and create a build folder for the output. Click configure, then generate, and choose the generator (VisualStudio, Eclipse, ...)

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
  • 5
    Please don't recommend using "link_directories" -- instead, give the full path names to any libraries required as arguments to target_link_libraries. The docs for link_directories ( http://cmake.org/cmake/help/v2.8.10/cmake.html#command:link_directories ) say "Note that this command is rarely necessary." – DLRdave Dec 22 '12 at 00:25
  • 1
    You can omit the NAME_HEADERS -- CMake scans source files to determine header file dependencies. – Colin D Bennett Nov 02 '13 at 16:55
  • 2
    @DLRdave In Windows you will need `link_directories(${OpenCV_LIB_DIR})` because the value returned in `${OpenCV_LIBS}` only include the library names (not the full path). If you want `${OpenCV_LIBS}` to actually include the library paths then you'll need `find_package(OpenCV REQUIRED core imgproc ...)`. When building statically that implies adding every single opencv module in the `find_package` call. – Darien Pardinas Jul 07 '15 at 13:37
  • 1
    Interesting...... Thanks for the info, I did not realize that. It's against the norm, too, though. The OpenCV find_package call ought to return full path names to the libraries in the variable. The VAST majority of other find_package calls yield full path names to libraries. – DLRdave Jul 07 '15 at 14:41
  • Dlls not found configuring for Visual Studio – Sergei Krivonos Sep 07 '17 at 08:21
  • 1
    This answer is incomplete and does not work. It does not even reference the OpenCV include directories. – Hermann May 04 '20 at 20:36
26

I am using opencv3.0 and cmake3.8, config below work for me!

######## A simple cmakelists.txt file for OpenCV() #############  
cmake_minimum_required(VERSION 2.8)                            
PROJECT(word)                                         

FIND_PACKAGE( OpenCV REQUIRED )                              
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )

ADD_EXECUTABLE(word main.c)                         
TARGET_LINK_LIBRARIES (word ${OpenCV_LIBS})         
########### end ####################################  
404pio
  • 1,080
  • 1
  • 12
  • 32
姚嘉辉
  • 477
  • 5
  • 5
  • 4
    apart from 'include_directories()' call is not required for CMake >= 2.8.11 this is the only right way to go. And the self-accepted answer is totally misleading to say at least. – Slava Sep 27 '17 at 08:42