示例(在編寫CMakeLists.txt前, 建立好src, include, lib, bin, build目錄, 其中cmake ..在build目錄中執行)
# 一般都會先聲明cmake的版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(hello_world)
# Release 和 Debug
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
# SET()用于指派, 類似于 int a = 100
# SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# AUX_SOURCE_DIRECTORY也是用于指派, 隻不過是特定于目錄的
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/src SRC_DIR)
# 設定頭檔案路徑
INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}/include
)
# 指明編譯的檔案, 建立可執行檔案
ADD_EXECUTABLE(main ${SRC_DIR})
# 在MakeFile檔案中生成install僞目标指令
INSTALL(TARGETS main DESTINATION ${PROJECT_SOURCE_DIR}/bin)
INSTALL(FILES ${SRC_DIR} DESTINATION ${PROJECT_SOURCE_DIR}/share)
# 指明編譯的檔案, 建立庫,
ADD_LIBRARY(libname SHARED lib_source_files)
# 添加庫的查找目錄
LINK_DIRECTORIES(/usr/local/lib)
# 連接配接庫檔案
TARGET_LINK_LIBRARIES(main lib_name)
# find_package指令, 讓cmake自己找庫
FIND_PACKAGE(OpenCV 3 REQUIRED)
# 使用C++的版本
ADD\_DEFINATION(-std=c++)
# 指明編譯器
SET(CMAKE\_C\_COMPILER g++)
cmake等幫助
cmake --help-command-list 檢視指令
cmake --help-variable-list 檢視内置變量
OpenCV的配置
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(demo)
SET(CMAKE_BUILD_TYPE Release)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# 放cmake自己找, 加載子產品成功之後就會新增加一些變量, OpenCV_LIBS, OpenCV_INCLUDE_DIRS, OpenCV_LIBRARY_DIRS
FIND_PACKAGE(OpenCV 3.4.1 REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
LINK_DIRECTORIES(${OpenCV_LIBRARY_DIRS})
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/src SRC_DIR)
ADD_EXECUTABLE(main ${SRC_DIR})
# OpenCV比較特殊, 下載下傳了V3的, 裡面會有V2和V3多個版本在一個目錄下, 通過OpenCV_LIBS明确是哪些庫, 接着直接連接配接, 是以我們也不需要上面的LINK_DIRECTORIES
TARGET_LINK_LIBRARIES(main ${OpenCV_LIBS})
INSTALL(TARGETS main DESTINATION ${PROJECT_SOURCE_DIR}/bin)