1. 程式人生 > >Linux系統使用入門進階總結(7)——CMake簡單的編寫規則

Linux系統使用入門進階總結(7)——CMake簡單的編寫規則

文章轉自:
https://blog.csdn.net/VennyJin/article/details/84995621

本篇以使用CMakeLists.txt構建一個簡單的OpenCV程式為例,簡單介紹一下CMakeLists.txt的編寫規則,各位讀者若後期有進一步的需求,強烈推薦一本叫《CMake Practice》的書,書中的例子會更加深入,也更加適合大型專案的構建。

CMake的編寫規則

CMakeLists.txt檔案內容 引用[1]

cmake_minimum_required(VERSION 3.10)
project(project_name)


# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)


# 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)
include_directories( ${OpenCV_INCLUDE_DIRS} )  #新複製過來的是沒有這一行的,自己加的
# Declare the executable target built from your sources
add_executable(project_name filename.cpp)

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

版本2

cmake_minimum_required(VERSION 2.8)    					//cmake版本號
project( DisplayImage )  								//專案名
find_package( OpenCV REQUIRED ) 							//找到opencv庫的位置 
add_executable( DisplayImage DisplayImage.cpp )  			//原始檔變成可執行檔案
target_link_libraries( DisplayImage ${OpenCV_LIBS} )		//連結庫

編寫完了之後,隨便來一個原始檔

進入命令列,輸入以下命令

mkdir build		//這個是外部構建,好處就是把編譯過程中的中間檔案全部放到build目錄下,保證工程簡潔
cd build
cmake ..		//因為cmakelists在上一目錄下
make
./DisplayImage

[1][https://blog.csdn.net/xihuanzhi1854/article/details/81635249