C++工程(二):学习使用CMake构建C++工程

CMake --> Makefile/Ninja/MSVC

CMake解决了makefileMSVC不能跨平台的问题,ninja可以适用于LinuxWindows系统

cmake -S . -B build -D  # 指定Release/Debug

cmake --build build

CMake常用语法:

  • add_library
  • add_executable

Example 1

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Define project name
project(opencv_example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})

总结Summary

CMake相较于Makefile 的优势:

  • CMake可以跨平台(Makefile for Linux)
  • CMake的底层即为Makefile (Darknet版本的YOLO就是用makefile来组织工程的)

应知应会

posted @ 2023-02-02 14:17  梦一场6688  阅读(130)  评论(0)    收藏  举报