Generate very simple program using OpenCV with CMake (C++)
This page describes the procedure to make very simple OpenCV application using CMake.
Environment:
OpenCV 4.6
CMake 3.16.3
Ubuntu 20.04
Procedure#
Assuming you have application source codes as main.cpp and header.h in the directory structure like this. CMakeLists.txt will be written after.
./
+--- CMakeList.txt
+--- src/
| +--- main.cpp
+--- include/
+--- header.h
Write the codes below in CMaekLists.txt .
cmake_minimum_required(VERSION 3.4.3)
set(PROJECT_NAME opencv_app)
project(${PROJECT_NAME} LANGUAGES CXX)
set(SRC_FILES
"src/main.cpp")
set(HEADER_FILES
"include/header.h")
list(APPEND CMAKE_MODULE_PATH
"${CMAKE_SOURCE_DIR}/cmake")
# find OpenCV package
find_package(OpenCV REQUIRED)
# set up the location of OpenCV include dirs
include_directories(${OpenCV_INCLUDE_DIRS})
# specify the executable to build with this CMake file
add_executable("${PROJECT_NAME}" "${SRC_FILES}" "${HEADER_FILES}")
# specify OpenCV library dirs, which should be put after add_executable
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
Description#
It is a common procedure to use find_package to include external libraries. If you are to create other CMake projects, the procedure is roughly the same though some tweaks are needed since some libraries does not have variables used in this page.
find_package#
find_package is the command which literaly find the external package. The arguments for the command is as below:
find_package(<PackageName> [version] [EXACT] [QUIET] [MODULE]
[REQUIRED] [[COMPONENTS] [components...]]
[OPTIONAL_COMPONENTS components...]
[REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
[GLOBAL]
[NO_POLICY_SCOPE]
[BYPASS_PROVIDER])
I will explain the minimum required atguments for using the function.
Name | Description |
---|---|
PackageName | The name of the pacakge to be included |
version | A single version or a range of version required |
REQUIRED | This value is set when the external package is necessary for the project |
I don't use version in the example above, it makes the CMake project more robust by specifying the version in the arguemnt. For long and widely used library like OpenCV, it is recommended to specify version to avoid unintentionally linking the old one.
include_directories#
include_directories is the CMake command which specifies the files to include for the compiler.
The variable named OpenCV_INCLUDE_DIRS which is specified for this command in the example is the automatically generated variable when you find_package OpenCV. The variable contains paths for OpenCV header files[1] .
target_link_libraries#
target_link_libraries is the cmake command to specify which libraries to be linked when build the target program for the compiler.
The variable named OpenCV_LIBS is the automatically generated variable when you find_package OpenCV. The variable contains paths for OpenCV library files[1] .
If you dump the contents of the variable, you can see many libraries listed. But the outcome file is just linked to the minimum required libraries. You can check that by ldd command.
You can refer to the sample project of OpenCV!#
If you built your OpenCV from the source codes, you have samples of CMakeLits.txt. You can check them in the file like samples/cpp/example_cmake/CMakeLists.txt in source package. It'll give you more information around CMake and OpenCV.