OpenCV and CMake

Sep 14, 2010 by     7 Comments    Posted under: Linux/Unix, OpenCV, other, Tutorials

From OpenCV 2.1 version we can use CMake to create and manage our OpenCV projects.

CMake is a cross-platform and open-source build system, and it’s used to control our compilation process, using a simple text files for define compilation process with independent platform and compiler.

Thanks to CMake we can create our project in our operating system as linux compile and work in it, and then use it to compile a new version in other os as windows, osx, …, even create a visual studio project, xcode project or eclipse.

To create a basic project with 1 main.c file with cmake and opencv we must create a new file called CMakeList.txt.

In this file we go to define the compiler process and his requeriments.

First we need create a project name:

PROJECT( project_name )

We need find OpenCV libraries. OpenCV is required by our project and it’s defined with REQUIRED statement:

FIND_PACKAGE( OpenCV REQUIRED )

Then we must defined our executable, his  name and the dependent files, in our example only main.c, and we go to use the project name for the executable name. This name is stored in ${PROJECT_NAME} variable.

ADD_EXECUTABLE( ${PROJECT_NAME} main.c )

To finish, we must link OpenCV to our executable, to do this only need call TARGET_LINK_LIBRARIES statement, with the project name and OpenCV library.

TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )

The CMakeList.txt was defined, then we can compile our project. To do this only need create a directory where we want get our executable. Then  we call cmake with the directory where  CMakeList.txt and main.c files are saved.

When cmake finish whe can call make to compile our project under unix.

If we want create a VisualStudio project under Windows, only need open CMake Gui and select the source code and build directories and cmake detect our visualstudio version and with generate button create our VisualStudio project.

Sample Code:

PROJECT( Nombre_del_proyecto )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( ${PROJECT_NAME} main.c )
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )

7 Comments + Add Comment

  • And if you need to check the OpenCV version you can use this ;)

    IF (${OpenCV_VERSION} VERSION_LESS 2.0.0)
    MESSAGE(FATAL_ERROR “OpenCV version is not compatible : ${OpenCV_VERSION}”)
    ENDIF()

  • TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )
    should be:
    TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} )

    and CMakeList.txt needs to be CMakeLists.txt

    Mark

  • how to create a trainning data into file xml using boosting? I really appreciate you for helping me.
    If possible, can you send me some document to do.
    Thank you

  • You use the cvMat for train boosting, then when you have this mat created, save it in xml or yaml with opencv functions http://opencv.willowgarage.com/documentation/cpp/core_xml_yaml_persistence.html

  • On linux in a GTK environment I use Anjuta. It is a young IDE, but has potential and integrates well with Gnome. It is written in C/C++ so its not a slow Java IDE.

  • Is this can run on ios

  • [...] you through the process of using CMake to compile and build OpenCV projects. It is based on the tutorial made by Damilesbut has some minor [...]

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Category

Polls

How Is My Site?

View Results

Loading ... Loading ...

Twitter: damiles3D