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:


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.


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.


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 )