How to use CMake with OpenCV

CMake is a cross platform, open-source build system. It consists in a family of tools designed to build, test and package software. It is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates makefiles that can be used in the compiler environment of your choice.

OpenCV is an open-source computer vision library which was already mentioned in this blog (see How to install OpenCV 2.3.1 in Ubuntu 11.10 Oneiric Ocelot with Python support).

This "how to" will guide you through the process of using CMake to compile and build OpenCV projects. It is based on the tutorial made by Damiles but has some minor changes.
1. Create a file named CMakeLists.txt and open it for edition in your favourite text editor
2. Specify the minimum required CMake version
CMAKE_MINIMUM_REQUIRED( VERSION x.x.x )
3. Create a project name
PROJECT( project_name ) 
4. Find the OpenCV libraries. OpenCV is required by the project and it is defined with the REQUIRED statement
FIND_PACKAGE( OpenCV REQUIRED ) 
5. Define the executable, his name and the dependent files. In this example we'll only have main.c and we'll use the project name for the executable name. The project's name is stored in the $(PROJECT_NAME) variable.
ADD_EXECUTABLE ( ${PROJECT_NAME} main.c )
6. Link OpenCV to the executable. To do it you only need to call TARGET_LINK_LIBRARIES with the project name and the OpenCV library.
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )
7. (Optional) Add a custom command to be run after building the target. This step may be useful if you want to copy or rename or do anything else with the built files after compilation. In this example we'll copy the generated executable and add the ".o" extension to it
ADD_CUSTOM_COMMAND( TARGET ${PROJECT_NAME} POST_BUILD COMMAND cp ${PROJECT_NAME} ${PROJECT_NAME}.o
You can also add custom commands for PRE_BUILD (run before all other dependencies) and PRE_LINK (run after other dependencies). For more information on this check the CMake docs on add_custom_command.
8. Save the file in the same folder where your source code is located. The file should look like this
CMAKE_MINIMUM_REQUIRED(VERSION x.x.x)
PROJECT( project_name )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( ${PROJECT_NAME} main.cpp )
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} )
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND cp ${PROJECT_NAME} ${PROJECT_NAME}.o )
9.If you want to build it in the same folder just run
cmake .
If you want to build the project in a different folder such as a "build" folder located where you have your source code, enter the "build" folder and run
cmake ..
For more information you can also check the Getting started page in the OpenCV wiki.

Update (26/10/11): I placed an entry on this blog on How to use OpenCV with CMake and Codeblocks on Ubuntu so that you can setup a complete development/debugging environment. Check it out if you want.

Update (18/11/11): I placed an entry on this blog on How to use OpenMP with CMake so that you can use the OpenMP API for parallel programming in your projects. If you may find it useful, check it out!



Did you find this post helpful? Do you wish to contribute to other projects regarding computer science, electronics, robotics or mechatronics that will be posted in this blog? If you wish to do so, you can donate via paypal using the button below. Thanks! :)

Donate

6 comments:

  1. You're welcome :)

    ReplyDelete
  2. I'm having a trouble with this.

    it says fatal error: cv.h: No such file or directory

    can you help please?
    ty

    ReplyDelete
  3. Hi

    Is there any guide to How to install and use OpenCV with CMake and Codeblocks on Oracle Enterprise Linux 5 ?

    We have issues trying to cmake, let alone getting opencv to run.

    ReplyDelete
    Replies
    1. Hi! I'm afraid I can't really help you with that :\ I never tried any version of Oracle Enterprise Linux :\ However, I'm sure that there must be some way of installing everything. Sorry that I can't provide more help :\ Good luck!

      Delete

top