官术网_书友最值得收藏!

Generating a CMake script file

Before we start creating our source file, we are going to generate the CMakeLists.txt file to allow us to compile our project, structure it, and execute it. The following CMake script is simple and basic but enough to compile and generate the executable:

cmake_minimum_required (VERSION 3.0)

PROJECT(Chapter4_Phototool)

set (CMAKE_CXX_STANDARD 11)

# Requires OpenCV
FIND_PACKAGE( OpenCV 4.0.0 REQUIRED )
MESSAGE("OpenCV version : ${OpenCV_VERSION}")

include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})

ADD_EXECUTABLE(${PROJECT_NAME} main.cpp)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})

The first line indicates the minimum CMake version required to generate our project, the second one sets the project name that we can use as the ${PROJECT_NAME} variable, and the third one sets the required C++ version; in our case, we require the C++11 version, as we can see in the next snippet:

cmake_minimum_required (VERSION 3.0)

PROJECT(Chapter4_Phototool)

set (CMAKE_CXX_STANDARD 11)

Moreover, we require the OpenCV library. First, we need to find the library, and then we'll show a message on the OpenCV library version found with the MESSAGE function:

# Requires OpenCV 
FIND_PACKAGE( OpenCV 4.0.0 REQUIRED ) 
MESSAGE("OpenCV version : ${OpenCV_VERSION}") 

If the library, with a minimum version of 4.0, is found, then we include the headers and library files in our project:

include_directories(${OpenCV_INCLUDE_DIRS}) 
link_directories(${OpenCV_LIB_DIR})

Now, we only need to add the source files to compile and link with the OpenCV library. The project name variable is used as the executable name, and we use only a single source file, called main.cpp:

ADD_EXECUTABLE(${PROJECT_NAME} main.cpp) 
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})
主站蜘蛛池模板: 乡宁县| 通城县| 霍州市| 和顺县| 海门市| 安溪县| 武宁县| 舞阳县| 隆安县| 边坝县| 台湾省| 界首市| 卢氏县| 南昌市| 兰考县| 防城港市| 岗巴县| 布拖县| 彝良县| 碌曲县| 宁远县| 桐城市| 吉林省| 榆林市| 曲阜市| 肥城市| 霸州市| 古蔺县| 太仓市| 楚雄市| 政和县| 留坝县| 佳木斯市| 霍州市| 武冈市| 徐水县| 正安县| 万全县| 朝阳区| 临海市| 兴安县|