1. 程式人生 > >CMakeLists.txt

CMakeLists.txt

成功 minimum dir ets deb bin usr 聲明 --help

示例(在編寫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)

CMakeLists.txt