Originally thought in Ubuntu installs after OpenCV, oneself writes a simple program should be very easy, but, is in order to compile a simple display picture The program I all quickly to be broken up.
After looking at the answers to the questions on Google and StackOverflow, I'll introduce command line and CMake two ways.
First I'll stick to the code I tested, the file name is test.c
#include
First method: Command line (methods using command-line arguments)
GCC test.c-o Test ' pkg-config--cflags--libs opencv '
./test
Here I explain a little bit about the role of this command.
In the above compilation command we actually use a tool "Pkg-config", it mainly has the following functions: Check the library version number. If the desired version of the library does not meet the requirements, it prints out the error message and avoids linking the wrong version of the library file. Gets the compilation preprocessing parameters, such as macro definitions, and the location of the header file. Get the link parameters, such as the location of the library and other dependent libraries, the filename, and some other connection parameters. Automatically join the settings of other libraries on which you depend
All of our compilations are handy after we have this tool (but before you make sure that you have a Pkgconfig folder in the directory of the OPENCV installation link library files that you install, In this folder there is a opencv.pc file, in fact, this is the pkg-config under the OpenCV configuration file.
When using Pkg-config, the option--cflags is used to specify the directory in which the program needs the header file at compile time, and the option--libs specifies the directory of the dynamic link libraries that the program needs to link to. For example, the following picture shows the OpenCV directory on my Computer.
The second method: using the CMake tool recommended by OPENCV
This method, I depressed for a long time, tried a variety of methods, the final compilation of the success of the operation. A sad tears ah.
Here's a step-by-step introduction to the next step:
Step1: Create a new directory to store our code and the relevant pictures to be processed in the program
Step2: Add the files required by the CMake tool at compile time CMakeLists.txt
The specific contents of the CMakeLists.txt file are as follows (note: This file can be copied to the/samples/c/example_cmake/folder under the folder you extracted from your OPENCV source code and then modified).
Let me briefly introduce the general meaning of the content here
Project (Opencv_example) //This is to build a project (similar to our C + + project in VS), the project name in parentheses, the project name we can give arbitrarily, the final program compiled the executable file is this name
CMAKE _minimum_required (version 2.6)//This is the minimum version requirement for the CMake tool, here we want to check the version information of our CMake tool, we can use the command "CMake--version" to view
if ( Command Cmake_policy)
cmake_policy (SET CMP0003 NEW)
endif (command cmake_policy)
Find_package (Open CV REQUIRED) //This is cmake used to find OpenCV packages without changing
# Declare the target (an executable)
add_executable (opencv_ Example image_show.c) //Here in parentheses, the two parameters are the project name and we want to compile the meaning of the file name, remember the middle one space bar separated
target_link_libraries (opencv_ Example ${opencv_libs}) //This is the link we link to the OpenCV library, we only need to change the first parameter in front of our project name can
#MESSAGE (STATUS "Opencv_libs: ${ Opencv_libs} ") //Well, just modify something like that, save it, close it.
Then we switch the terminal's working directory to the directory where we built the project files.
(note, be sure to note: You must at this time to see the installation of the OPENCV you set up the compile file, there is no sign with the lock (that is, you compile the OpenCV is compiled with root permissions), on this point I was miserable, such as my computer on the show is like this
See the Build folder in the picture above, that's the folder I created when I compiled the OPENCV source code. )
So, if you're compiling with root permissions, you're going to switch to root and switch to the current working directory OPENCV. If you do not compile the OpenCV with root permissions, you can switch directories directly.
My computer is going to be switched to the root state.
STEP3: Let's Enter the command "CMake." Compile the current project. Ah oh, then you'll find a bug in your compile
The general meaning of this warning is that CMake cannot find the OpenCV link library file, you need to manually set the Opencv_dir to point to the path containing the library file.
Well, this is the most painful place, on this issue I Google and on the StackOverflow have not found the answer, can only try, but still find some tips from netizens.
In fact, through it to give us the hint we just want to set up this path. Let's go through the Camke QT interface to set it up.
STEP4: Setting Opencv_dir
(1) First install the CMake qt interface "sudo apt-get install Cmake-gui"
(2) Open the Cmake-gui interface
(3) We are selecting our directory in the option of where is the source code and where to build binaries just set up the file path that contains our program.
(4) We select the "Add Entry" option in the CMake interface and enter the information in the way I pictured below, where the Alue value is the directory I installed OPENCV (recall what I said above).
So we'll just click OK and get the picture below.
To this opencv_dir configuration is complete, click the following configure and generate, if not the error is all done. We can happily go back to the terminal to compile our program.
(5) The method of compiling is as shown in the figure
See, the Green Opencv_example is the executable file we just built.
STEP5: Run the test.
All right, it's done.
Reprint please indicate the source: http://www.cnblogs.com/woshijpf/