当前位置:网站首页>CMake 基础学习
CMake 基础学习
2022-07-28 22:13:00 【黄昏贩卖机】
CMake 简介
CMake 是一个跨平台、开源的构建系统,一个集软件构建、测试、打包于一身的软件。它使用与平台和编译器独立的配置文件来对软件编译过程进行控制。
一 初识CMake
使用一个十分简单的例子:
在新建的项目文件夹下,建立如下两个文件
$ tree
.
├── CMakeLists.txt
└── main.cpp
CMakelists.txt 中的文件内容如下
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (hello_cmake)
# Add an executable
add_executable(hello_cmake main.cpp)
main.cpp 中是一个简单的C++程序
#include<iostream>
using namespace std;
int main(){
cout <<" hello word !"<<endl;
return 0;
}
关于CMakelists.txt 中的内容后面再解释,我们先将整个程序编译运行出来。
二进制目录
运行cmake命令 的根文件夹或者顶级文件夹称为 CMAKE_BINARY_DIR,是所有二进制文件的根文件夹。CMake 及支持就地构建和生成二进制文件,也支持在源代码外构建和生成二进制文件。
就地构建
就地构建会在源代码所在的文件夹生成所有的临时文件,使得源代码和临时文件都混在一起,这样会很不方便。
我们执行 cmake . 命令, 注意符号 “ . ” 表示在当前目录执行。
$ cmake .
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/panlichen/zrk/hello_cmake
$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.18.2
│ │ ├── CMakeCCompiler.cmake
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeSystem.cmake
│ │ ├── CompilerIdC
│ │ │ ├── a.out
│ │ │ ├── CMakeCCompilerId.c
│ │ │ └── tmp
│ │ └── CompilerIdCXX
│ │ ├── a.out
│ │ ├── CMakeCXXCompilerId.cpp
│ │ └── tmp
│ ├── cmake.check_cache
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── hello_cmake.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── DependInfo.cmake
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ └── progress.make
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.cpp
└── Makefile
8 directories, 28 files
可见已经成功生成了makefile文件,接下来我们make一下,并执行
$ make
$ ./hello_cmake
hello word !
当修改代码时,没有增减源文件修改CMakeLists.txt 只需要重新make
源外构建 (推荐)
使用源外构建,你可以创建单个生成文件夹,该文件夹可以位于文件系统的任何位置。所有临时构建的目标文件都位于此目录中,以保持源码目录结构的整洁。
我们创建一个build 文件夹,在build 文件夹下执行 命令:
cmake + CMakeLists.txt 所在路径
$ tree ..
..
├── build
├── CMakeLists.txt
└── main.cpp
$ cmake ..
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/panlichen/zrk/hello_cmake/build
$ make
Scanning dependencies of target hello_cmake
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
hello word !
源外构建后,目录结构如下
$ tree ..
..
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.18.2
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── hello_cmake.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── CXX.includecache
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.internal
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── main.cpp.o
│ │ │ └── progress.make
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ ├── hello_cmake
│ └── Makefile
├── CMakeLists.txt
└── main.cpp
9 directories, 32 files
边栏推荐
- EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
- Compose 的声明式代码如此简洁?
- Leetcode63. 不同路径 II
- VMware VCSA 7.0 Install
- Worthington -- Specification of Worthington trypsin inhibitor
- SQL left connection, internal connection writing method "recommended collection"
- JS高级 之 ES6~ES13 新特性
- [detailed and super simple] how to use websocket links
- C language n*n matrix evaluation and inverse matrix [easy to understand]
- 小程序editor富文本编辑使用及rich-text解析富文本
猜你喜欢

剑指 Offer 55 - I. 二叉树的深度

Leetcode60. 排列序列

JS高级 之 ES6~ES13 新特性

有效供应链管理的八大绩效分析指标(上)

Multi sensor fusion positioning (II) -- map based positioning

field injection is not recommended 的解决办法

Blocking queue

Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan

AUTOCAD——Excel表格导入CAD、CAD合并两兄弟

Connection pool - return connection details (Part 2)
随机推荐
台式机dp接口在哪(主机没有dp接口怎么办)
Word中的\n是什么?:^p
以JSP为视图解析器搭建SSM项目
实时数仓:网易严选基于Flink的实时数仓实践
GhostNets on Heterogeneous Devices via Cheap Operations
EN 1935建筑五金.单轴铰链—CE认证
EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
Worthington丨STEMxyme的分类和测定方法
Zero view h5s video platform getUserInfo information disclosure vulnerability cnvd-2020-67113
【CNN】为什么CNN的卷积核大小一般都是奇数
Ape anthropology topic 20
【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
Is the declarative code of compose so concise?
Pycharm new project
器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
Explanation of history and chemical properties of Worthington ribonuclease B
CANoe应用案例之DoIP通信
Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click
Xss.haozi.me range details
VS2005透过SourceOffSite访问VSS2005的设置方法「建议收藏」