1 Using OpenCV with gcc and CMake {#tutorial_linux_gcc_cmake}
2 ===============================
4 @note We assume that you have successfully installed OpenCV in your workstation.
6 - The easiest way of using OpenCV in your code is to use [CMake](http://www.cmake.org/). A few
7 advantages (taken from the Wiki):
8 -# No need to change anything when porting between Linux and Windows
9 -# Can easily be combined with other tools by CMake( i.e. Qt, ITK and VTK )
10 - If you are not familiar with CMake, checkout the
11 [tutorial](http://www.cmake.org/cmake/help/cmake_tutorial.html) on its website.
16 ### Create a program using OpenCV
18 Let's use a simple program such as DisplayImage.cpp shown below.
21 #include <opencv2/opencv.hpp>
25 int main(int argc, char** argv )
29 printf("usage: DisplayImage.out <Image_Path>\n");
34 image = imread( argv[1], 1 );
38 printf("No image data \n");
41 namedWindow("Display Image", WINDOW_AUTOSIZE );
42 imshow("Display Image", image);
49 ### Create a CMake file
51 Now you have to create your CMakeLists.txt file. It should look like this:
53 cmake_minimum_required(VERSION 2.8)
54 project( DisplayImage )
55 find_package( OpenCV REQUIRED )
56 include_directories( ${OpenCV_INCLUDE_DIRS} )
57 add_executable( DisplayImage DisplayImage.cpp )
58 target_link_libraries( DisplayImage ${OpenCV_LIBS} )
60 ### Generate the executable
62 This part is easy, just proceed as with any other project using CMake:
64 cd <DisplayImage_directory>
70 By now you should have an executable (called DisplayImage in this case). You just have to run it
71 giving an image location as an argument, i.e.:
73 ./DisplayImage lena.jpg
75 You should get a nice window as the one shown below:
77 ![](images/GCC_CMake_Example_Tutorial.jpg)