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

猜你喜欢

转载自blog.csdn.net/VennyJin/article/details/84995621