cmake logo. source: cmake.org |
Unlike
Python that we can run any packages methods directly when all
packages needed are installed, using another Programming, like in
this case C++, we need to configure some packages and libraries. Here
I don’t intend to compare any programming languages but just want
to share about compiling OpenCV with C++ using CmakeFiles.txt.
There
are many ways to compile OpenCV library on linux operating system. By
Using IDE (Integrated Development Environment) such as Eclipse,
Netbeans, Cmake, and many more. But for this post will just be
discussing about the last one; using Cmake with a simple example. The
example going to be used here is “detecting basic shape” program
from the last post:
Detecting
Basic Shape with OpenCV by Corners Quantity
Now for the first step is hierarchical files:
--build #folder where files are compiled
----main.cpp #file to be compiled
----CMakeLists.txt #a ’tool’ to compile
----tri.png
The main part here is CMakeLists.txt which will generate a file named
Makefile, this file is then used to generate an
executable file. Note that the name of the file must be exactly like
written above. It is a mistake if written to be CmakeLists.txt,
cmakelists.txt, CMAKELISTS.TXT, and so on. Any other files like
main.cpp and tri,png can be placed on whenever destination that we
want, because they can be configured later on the CMakeLists.txt
file. But for simplicity the hierarchical folder is expressly made
like above.
For the build folder, its name doesn’t have to be ‘build’,
you can freely give a whatever name you want. The ‘build’ name
here is just a term frequently used for this purpose. Finally, let’s
see the content of CMakeLists.txt file that is used to generate
Makefile:
# version of cmake going to use
cmake_minimum_required(VERSION 3.5.1)
# Define project name
project(DETECT_SHAPE)
#set language flag
set(CMAKE_CXX_FLAGS "-std=c++11 -Os -Wall")
# Find OpenCV
find_package(OpenCV REQUIRED)
# Print a message: opencv version
message(STATUS "version: ${OpenCV_VERSION}")
# Declare the executable target built from your sources
add_executable(${PROJECT_NAME} main.cpp)
# Link your application with OpenCV libraries
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
The cmake_minimum_required() is where the version of cmake that we
are going to use, note that cmake needs this line. If not defined, by
default the lower version might be used, and for that, some of c++11
features would not function properly or even exerted some error
messages when compiled. The next line, set() is used to set any cmake
variable. Here the CMAKE_CXX_FLAGS is set by some parameters:
- std=c++11: we use c++11 facilities
- Os: optimize the code generated
- Wall: show all warning if found
The OpenCV package can simply be found by find_package() by passing
‘OpenCV REQUIRED’ argument to it. The next line is for a message
that we want to print. This line is optional, whether applied or not,
our project code generated will not be affected. In this case, STATUS
is assign by OpenCV_VERSION that will show version of OpenCV that we
are going to use.
The following line is add_executable(). This is where the executable
file of our project is generated. The arguments passed are simply the
name of the project followed by any .c or .cpp files that is going to
be compiled. The argument ${PROJECT_NAME} will produce name of our
project, that is, a name we passed into project() above, there we
give DETECT_SHAPE as a name, and therefore the name of our executable
file is DETECT_SHAPE.
And for the last line is target_link_libraries() that links OpenCV
library to our project file(s). The argument needed is simply the
project name followed by any library name. For this case,
${OpenCV_LIBS} is passed.
Now lets jump to the compiling steps. For the first step is of course
the hierarchical files must be set like above. You can find the
main.cpp code on another post linked above. Now open your terminal
and go to build folder of this project and type:
cmake..
the information on your terminal must produce something like this:
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "4.1.1")
-- version: 4.1.1
-- Configuring done
-- Generating done
-- Build files have been written to: /media/…
In this step, the Makefile has been produced. Next just simply type
‘make’ to generate an executable file:
make
the line will produce:
Scanning dependencies of target DETECT_SHAPE
[ 50%] Building CXX object CMakeFiles/DETECT_SHAPE.dir/main.cpp.o
[100%] Linking CXX executable DETECT_SHAPE
[100%] Built target DETECT_SHAPE
In this step, the compilation and linking libraries are done.
And the last step is to run the executable file by:
./DETECT_SHAPE
A triangle object shape detected is then shown.
0 Comments
Post a Comment