fd447307a798a354e2edc5c03ba99df2f7e41589
[platform/upstream/opencv.git] / doc / tutorials / introduction / linux_gcc_cmake / linux_gcc_cmake.markdown
1 Using OpenCV with gcc and CMake {#tutorial_linux_gcc_cmake}
2 ===============================
3
4 @note We assume that you have successfully installed OpenCV in your workstation.
5
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.
12
13 Steps
14 -----
15
16 ### Create a program using OpenCV
17
18 Let's use a simple program such as DisplayImage.cpp shown below.
19 @code{.cpp}
20 #include <stdio.h>
21 #include <opencv2/opencv.hpp>
22
23 using namespace cv;
24
25 int main(int argc, char** argv )
26 {
27     if ( argc != 2 )
28     {
29         printf("usage: DisplayImage.out <Image_Path>\n");
30         return -1;
31     }
32
33     Mat image;
34     image = imread( argv[1], 1 );
35
36     if ( !image.data )
37     {
38         printf("No image data \n");
39         return -1;
40     }
41     namedWindow("Display Image", WINDOW_AUTOSIZE );
42     imshow("Display Image", image);
43
44     waitKey(0);
45
46     return 0;
47 }
48 @endcode
49 ### Create a CMake file
50
51 Now you have to create your CMakeLists.txt file. It should look like this:
52 @code{.cmake}
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} )
59 @endcode
60 ### Generate the executable
61
62 This part is easy, just proceed as with any other project using CMake:
63 @code{.bash}
64 cd <DisplayImage_directory>
65 cmake .
66 make
67 @endcode
68 ### Result
69
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.:
72 @code{.bash}
73 ./DisplayImage lena.jpg
74 @endcode
75 You should get a nice window as the one shown below:
76
77 ![](images/GCC_CMake_Example_Tutorial.jpg)