[CMake]CMake快速入门step1

由于学习OpenCV的时候,官方的教程使用了CMake,学习CMake并记录笔记如下:

本笔记是根据官网的7 steps 教程修改补充得到的,但是官网的描述并不清楚,所以添加一些注释并补充一些CMake的语法:

官网的语法文档:https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#id9

官网的7 steps教学:https://cmake.org/cmake-tutorial/

A Basic Starting Point (Step1)

这个简单的例子将构建一个最小的使用cmake编译的工程

这个例子里只有一个待编译的文件,main.cpp

 

#include<iostream>
using namespace std;
int main(){

    cout<<"hello world"<<endl;
    return 0;
    
}

然后编写最简单的CMakeLists.txt文件

cmake_minimum_required(VERSION 2.6)
project (tutorial)  # set project's name as tutorial
add_executable (tutorial.exe main.cpp) 
#[[
    set the executable output file tutorial.exe 
    set the source file main.cpp 
]]

解释:

  1. CMake语法中,单行注释使用# comments,多行注释使用#[[  comments ]]
  2. 这个最简单的CMakeLists.txt只有三行
  3. 第一行cmake_minimum_required(VERSION 2.6)表示这个工程编译最低CMake版本2.6
  4. 第二行project (tutorial) 表示给这个工程命名为tutorial
  5. 第三行add_executable(tutorial.exe main.cpp)表示生成的可执行文件命名为tutorial.exe,使用源文件main.cpp

完成了以上两步,就完成了项目源代码和CMakeLists.txt的文件配置

查看此时的目录:

chenhanxuan@idiospace:~/Documents/OpenCV_Notes/CMake/samples/specimen1/n1$ ls
CMakeLists.txt  main.cpp

接下来只需要两个命令即可编译并输出可执行文件

cmake .

这一步是完成makefile的生成以及CMake的缓存文件的生成(缓存文件的作用在后面的教程中会提到)

生成的文件保存在当前的目录下

输入指令后会出现:

chenhanxuan@idiospace:~/Documents/OpenCV_Notes/CMake/samples/specimen1/n1$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chenhanxuan/Documents/OpenCV_Notes/CMake/samples/specimen1/n1

然后查看此时的目录:

chenhanxuan@idiospace:~/Documents/OpenCV_Notes/CMake/samples/specimen1/n1$ ls
CMakeCache.txt  cmake_install.cmake  main.cpp
CMakeFiles      CMakeLists.txt       Makefile

可以看到多出来了4个文件

然后输入指令

make
chenhanxuan@idiospace:~/Documents/OpenCV_Notes/CMake/samples/specimen1/n1$ make
Scanning dependencies of target tutorial.exe
[ 50%] Building CXX object CMakeFiles/tutorial.exe.dir/main.cpp.o
[100%] Linking CXX executable tutorial.exe
[100%] Built target tutorial.exe

可以看到生成最终可执行文件tutorial.exe的执行过程

查看此时的文件目录:

chenhanxuan@idiospace:~/Documents/OpenCV_Notes/CMake/samples/specimen1/n1$ ls
CMakeCache.txt  cmake_install.cmake  main.cpp  tutorial.exe
CMakeFiles      CMakeLists.txt       Makefile

运行最终的目标文件:

chenhanxuan@idiospace:~/Documents/OpenCV_Notes/CMake/samples/specimen1/n1$ ./tutorial.exe 
hello world

step1到这里结束了,接下来会讨论添加头文件和自己的函数库的配置和CMakeList.txt的写法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值