【Make编译控制 06】CMake初步使用

目录

一、概述与安装

二、编译源文件

三、无关文件管理


一、概述与安装

CMake是一个跨平台的项目构建工具,相比于Makefile,CMake更加高级,因为CMake代码在执行的时候是会先翻译生成Makefile文件,再调用Makefile文件完成项目构建,这就像是高级语言和低级语言之间的关系。

Makefile虽然也支持在不同的平台上运行,但是不同平台上进行项目构建的Makefile代码是不一样的,也就是说不具备Makefile代码不具备跨平台性,所以CMake的出现便是帮Makefile解决了这个问题,因为CMake可以根据不同的平台生成不同的Makefile文件。

Linux系统CMake直接命令行安装:yum install -y cmake

Windows系统CMake安装地址:https://cmake.org/files/

选择对应版本的 x86_64.msi 文件下载运行,安装的时候选择添加系统环境变量:

# 在PowerShell上检查是否安装成功PS C:\Users\pheonixFly> cmake --version
cmake version 3.28.0-rc1CMake suite maintained and supported by Kitware (kitware.com/cmake).
PS C:\Users\pheonixFly>

二、编译源文件

// add.h#pragma once
int add(int a, int b);
// sub.h#pragma once
int sub(int a, int b);
// add.cpp#include "add.h"int add(int a, int b) {return a + b;
}
// add.cpp#include "sub.h"int sub(int a, int b) {return a - b;
}
# CMakeLists.txt 文件代码cmake_minimum_required(VERSION 2.8)
# 指定使用的 cmake 的最低版本
# 可选,非必须,如果不加可能会有警告project(MATH)
# 定义工程名称,
# 并可指定工程的版本、工程描述、web主页地址、支持的语言(默认情况支持所有语言),
# 如果不需要这些都是可以忽略的,只需要指定出工程名字即可。add_executable(math.exe add.cpp sub.cpp main.cpp)
# add_executable(可执行程序名 源文件名称)
# 定义工程会生成一个可执行程序
# 源文件名可以是一个也可以是多个,如有多个可用空格或分号间隔
# 执行 cmake . 命令,生成 Makefile 文件
# 执行新生成的 Makefile 文件(base) [root@localhost 10_test]# tree .
.
├── add.cpp
├── add.h
├── CMakeLists.txt
├── main.cpp
├── sub.cpp
└── sub.h0 directories, 6 files
(base) [root@localhost 10_test]# cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /root/gitee/Test/Make_Learn/10_test
(base) [root@localhost 10_test]# tree .
.
├── add.cpp
├── add.h
├── CMakeCache.txt
├── CMakeFiles
│   ├── 2.8.12.2
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       └── CMakeCXXCompilerId.cpp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── math.exe.dir
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.cpp
├── Makefile         ################ 新生成的 Makefile 文件       
├── sub.cpp
└── sub.h6 directories, 32 files
(base) [root@localhost 10_test]# make
Scanning dependencies of target math.exe
[ 33%] Building CXX object CMakeFiles/math.exe.dir/add.cpp.o
[ 66%] Building CXX object CMakeFiles/math.exe.dir/sub.cpp.o
[100%] Building CXX object CMakeFiles/math.exe.dir/main.cpp.o
Linking CXX executable math.exe
[100%] Built target math.exe
(base) [root@localhost 10_test]# tree .
.
├── add.cpp
├── add.h
├── CMakeCache.txt
├── CMakeFiles
│   ├── 2.8.12.2
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       └── CMakeCXXCompilerId.cpp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── math.exe.dir
│   │   ├── add.cpp.o
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── CXX.includecache
│   │   ├── DependInfo.cmake
│   │   ├── depend.internal
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   ├── main.cpp.o
│   │   ├── progress.make
│   │   └── sub.cpp.o
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.cpp
├── Makefile         ################ 新生成的 Makefile 文件
├── math.exe         ################ 新生成的 math.exe 文件
├── sub.cpp
└── sub.h6 directories, 38 files
(base) [root@localhost 10_test]# ./math.exe
10 + 5 = 15
10 - 5 = 5
(base) [root@localhost 10_test]# 
# CMake 生成的 Makefile 文件代码# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target#=============================================================================
# Special targets provided by cmake.# Disable implicit rules so canonical targets will work.
.SUFFIXES:# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =.SUFFIXES: .hpux_make_needs_suffix_list# Suppress display of executed commands.
$(VERBOSE).SILENT:# A target that is always out of date.
cmake_force:
.PHONY : cmake_force#=============================================================================
# Set environment variables for the build.# The shell in which to execute make rules.
SHELL = /bin/sh# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake# The command to remove a file.
RM = /usr/bin/cmake -E remove -f# Escaping for special characters.
EQUALS = =# The program to use to edit the cache.
CMAKE_EDIT_COMMAND = /usr/bin/ccmake# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /root/gitee/Test/Make_Learn/10_test# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /root/gitee/Test/Make_Learn/10_test#=============================================================================
# Targets provided globally by CMake.# Special rule for the target edit_cache
edit_cache:@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."/usr/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast# Special rule for the target rebuild_cache
rebuild_cache:@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast# The main all target
all: cmake_check_build_system$(CMAKE_COMMAND) -E cmake_progress_start /root/gitee/Test/Make_Learn/10_test/CMakeFiles /root/gitee/Test/Make_Learn/10_test/CMakeFiles/progress.marks$(MAKE) -f CMakeFiles/Makefile2 all$(CMAKE_COMMAND) -E cmake_progress_start /root/gitee/Test/Make_Learn/10_test/CMakeFiles 0
.PHONY : all# The main clean target
clean:$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean# The main clean target
clean/fast: clean
.PHONY : clean/fast# Prepare targets for installation.
preinstall: all$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall# Prepare targets for installation.
preinstall/fast:$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast# clear depends
depend:$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend#=============================================================================
# Target rules for targets named math.exe# Build rule for target.
math.exe: cmake_check_build_system$(MAKE) -f CMakeFiles/Makefile2 math.exe
.PHONY : math.exe# fast build rule for target.
math.exe/fast:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/build
.PHONY : math.exe/fastadd.o: add.cpp.o
.PHONY : add.o# target to build an object file
add.cpp.o:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/add.cpp.o
.PHONY : add.cpp.oadd.i: add.cpp.i
.PHONY : add.i# target to preprocess a source file
add.cpp.i:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/add.cpp.i
.PHONY : add.cpp.iadd.s: add.cpp.s
.PHONY : add.s# target to generate assembly for a file
add.cpp.s:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/add.cpp.s
.PHONY : add.cpp.smain.o: main.cpp.o
.PHONY : main.o# target to build an object file
main.cpp.o:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/main.cpp.o
.PHONY : main.cpp.omain.i: main.cpp.i
.PHONY : main.i# target to preprocess a source file
main.cpp.i:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/main.cpp.i
.PHONY : main.cpp.imain.s: main.cpp.s
.PHONY : main.s# target to generate assembly for a file
main.cpp.s:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/main.cpp.s
.PHONY : main.cpp.ssub.o: sub.cpp.o
.PHONY : sub.o# target to build an object file
sub.cpp.o:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/sub.cpp.o
.PHONY : sub.cpp.osub.i: sub.cpp.i
.PHONY : sub.i# target to preprocess a source file
sub.cpp.i:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/sub.cpp.i
.PHONY : sub.cpp.isub.s: sub.cpp.s
.PHONY : sub.s# target to generate assembly for a file
sub.cpp.s:$(MAKE) -f CMakeFiles/math.exe.dir/build.make CMakeFiles/math.exe.dir/sub.cpp.s
.PHONY : sub.cpp.s# Help Target
help:@echo "The following are some of the valid targets for this Makefile:"@echo "... all (the default if no target is provided)"@echo "... clean"@echo "... depend"@echo "... edit_cache"@echo "... math.exe"@echo "... rebuild_cache"@echo "... add.o"@echo "... add.i"@echo "... add.s"@echo "... main.o"@echo "... main.i"@echo "... main.s"@echo "... sub.o"@echo "... sub.i"@echo "... sub.s"
.PHONY : help#=============================================================================
# Special targets to cleanup operation of make.# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

三、无关文件管理

通过上面的例子可以看出,在执行 cmake 命令的时候,会在当前目录生成一些与项目执行无关的目录和文件,如果再基于 Makefile 执行 make,还会生成更过的中间件文件,这样会导致整个项目变得混乱和难以管理,所以我们需要将通过 cmake 生成的新文件都统一放到一个目录里面进行管理,这个目录通常叫做 build

(base) [root@localhost 10_test]# mkdir build
(base) [root@localhost 10_test]# cd build
(base) [root@localhost build]# cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /root/gitee/Test/Make_Learn/10_test/build
(base) [root@localhost build]# tree .
.
├── CMakeCache.txt
├── CMakeFiles
│   ├── 2.8.12.2
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       └── CMakeCXXCompilerId.cpp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── math.exe.dir
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
└── Makefile    ####################### 在 build 目录下的 Makefile 文件             6 directories, 26 files
(base) [root@localhost build]# cd ..
(base) [root@localhost 10_test]# ls
add.cpp  add.h  build  CMakeLists.txt  main.cpp  sub.cpp  sub.h
(base) [root@localhost 10_test]# cd build/
(base) [root@localhost build]# make
Scanning dependencies of target math.exe
[ 33%] Building CXX object CMakeFiles/math.exe.dir/add.cpp.o
[ 66%] Building CXX object CMakeFiles/math.exe.dir/sub.cpp.o
[100%] Building CXX object CMakeFiles/math.exe.dir/main.cpp.o
Linking CXX executable math.exe
[100%] Built target math.exe
(base) [root@localhost build]# tree ..
..
├── add.cpp
├── add.h
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 2.8.12.2
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   └── CMakeCCompilerId.c
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       └── CMakeCXXCompilerId.cpp
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── math.exe.dir
│   │   │   ├── add.cpp.o
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── CXX.includecache
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.internal
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   ├── main.cpp.o
│   │   │   ├── progress.make
│   │   │   └── sub.cpp.o
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   ├── Makefile    ####################### 在 build 目录下的 Makefile 文件  
│   └── math.exe    ####################### 在 build 目录下的 math.exe 文件  
├── CMakeLists.txt
├── main.cpp
├── sub.cpp
└── sub.h7 directories, 38 files
(base) [root@localhost build]# ./math.exe
10 + 5 = 15
10 - 5 = 5
(base) [root@localhost build]# 

这样就可以在 build 目录中执行 make 命令编译项目,生成的相关文件自然也就被存储到 build 目录中了。这样通过 cmake 和 make 生成的所有文件就全部和项目源文件分离了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/463414.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EMC学习笔记(二十三)降低EMI的PCB设计指南(三)

双层板电源分配 1.单点与多点分布2.星型分布3.创建网格平面4.旁路和磁珠5.将噪声保持在芯片附近 tips:资料主要来自网络,仅供学习使用。 1.单点与多点分布 在一个真正的单点配电系统中,每个有源元件都有自己独立的电源和地,这些…

金融信贷风控决策引擎详解

风控决策引擎含义 风控决策引擎是对复杂的业务逻辑抽象化剥离出来的业务规则进行不同的分支组合、关联,然后层层规则递进运算,最终输出决策结果的产品。 传统的风控决策引擎主要实现规则的逻辑判断,例如:女厕所的规则可以制定成“…

Electron+Vue实现仿网易云音乐实战

前言 这个项目是我跟着官方文档的那个Electron入门教程大致跑了一遍,了解了下Electron开发流程之后的实战项目,所以中间应该是会有很多写法不是很规范,安全性有可能也没考虑到,可实现的各种api也不是很了解,适合初学者。 必须感谢 https://github.com/Binaryify/NeteaseC…

SFML(1) | 自由落体小球

小游戏和GUI编程(1) | 基于 SFML 的自由落体小球 文章目录 小游戏和GUI编程(1) | 基于 SFML 的自由落体小球1. 目的2. SFML 适合做图形显示的理由3. 使用 SFML - 构建阶段4. 使用 SFML - C 代码4.0 代码布局4.1 创建窗口4.2 循环显示窗口, 并处理关闭事件4.3 使用能够执行绘制的…

fast.ai 深度学习笔记(五)

深度学习 2:第 2 部分第 10 课 原文:medium.com/hiromi_suenaga/deep-learning-2-part-2-lesson-10-422d87c3340c 译者:飞龙 协议:CC BY-NC-SA 4.0 来自 fast.ai 课程的个人笔记。随着我继续复习课程以“真正”理解它,…

移动端web开发布局

目录 flex布局: flex布局父项常见属性: flex布局子项常见属性: REM适配布局: 响应式布局: flex布局: 需要先给父类盒子设置display:flex flex是flexiblebox的缩写,意为"弹…

【Java八股面试系列】并发编程-并发关键字,线程池

目录 并发关键字 Synchronized synchronized最主要的三种使用方式: 具体使用:双重校验锁单例模式 synchronized 底层实现原理? synchronized锁的优化 偏向锁 轻量级锁 重量级锁 Mark Word 与 Monitor 之间的关系 总结 偏向锁、轻量…

【MySQL】数据库基础 -- 详解

一、什么是数据库 存储数据用文件就可以了,为什么还要弄个数据库? 一般的文件确实提供了数据的存储功能,但是文件并没有提供非常好的数据(内容)的管理能力(用户角度)。 文件保存数据有以下几个缺点&…

fast.ai 深度学习笔记(一)

深度学习 2:第 1 部分第 1 课 原文:medium.com/hiromi_suenaga/deep-learning-2-part-1-lesson-1-602f73869197 译者:飞龙 协议:CC BY-NC-SA 4.0 来自 fast.ai 课程的个人笔记。随着我继续复习课程以“真正”理解它,这…

JAVA设计模式之策略模式详解

策略模式 1 策略模式概述 策略模式(strategy pattern)的原始定义是:定义一系列算法,将每一个算法封装起来,并使它们可以相互替换。策略模式让算法可以独立于使用它的客户端而变化。 其实我们在现实生活中常常遇到实现某种目标存在多种策略…

第9讲 详解第 2 套真题

第9讲 详解第 2 套真题 基本编程题【15 分】简单应用题【25 分】综合应用题【20 分】问题 1【10 分】:问题 2【10 分】:各位小伙伴想要博客相关资料的话关注公众号:chuanyeTry即可领取相关资料! 基本编程题【15 分】 考生文件夹下存在一个文件 PY101.py,请写代码替换横线,不…

vue3 之 通用组件统一注册全局

components/index.js // 把components中的所组件都进行全局化注册 // 通过插件的方式 import ImageView from ./ImageView/index.vue import Sku from ./XtxSku/index.vue export const componentPlugin {install (app) {// app.component(组件名字,组件配置对象)…