CMake3-常用功能3:cmake命令实现程序的分布生成【预处理(*.i C代码)➜编译(*.s汇编代码)➜汇编(*.o二进制代码)➜链接(*.out)➜运行(动态库加载路径)】

在这里插入图片描述
在这里插入图片描述

# CMakeLists.txt cmake文件名大小写不敏感

#指定cmake最低版本
cmake_minimum_required(VERSION 3.20)

#构建项目的名称
project(first_cmake)

#构建执行程序【whx: first_cmake为上面的项目名称】
add_executable(first_cmake 101first_cmake.cpp)

在这里插入图片描述

一、一步编译

1、生成构建所需的文件【cmake -S . -B build】

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake$ cmake -S . -B build
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/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: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake$ 

2、一步编译

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake$ cd build/
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ cmake --build .
[ 50%] Building CXX object CMakeFiles/first_cmake.dir/101first_cmake.cpp.o
[100%] Linking CXX executable first_cmake
[100%] Built target first_cmake
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ 

在这里插入图片描述

二、分步骤编译

1、生成构建所需的文件【cmake -S . -B build】

在 build 目录下生成构建所需的文件,准备进行编译

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake$ cmake -S . -B build
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/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: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake$ 

2、编译

进入build目录,查看

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ cmake --build . --target help
The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
... edit_cache
... rebuild_cache
... first_cmake
... 101first_cmake.o
... 101first_cmake.i
... 101first_cmake.s
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ 

2.1 预处理【生成*.i文件】

每一个.cpp都会生成一个.i文件

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ cmake --build . --target  101first_cmake.i
Preprocessing CXX source to CMakeFiles/first_cmake.dir/101first_cmake.cpp.i
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ 

在这里插入图片描述

# 0 "/home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/101first_cmake.cpp"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "/home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/101first_cmake.cpp"
# 26 "/home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/101first_cmake.cpp"
# 1 "/usr/include/c++/11/iostream" 1 3
# 36 "/usr/include/c++/11/iostream" 3
       
# 37 "/usr/include/c++/11/iostream" 3

# 1 "/usr/include/x86_64-linux-gnu/c++/11/bits/c++config.h" 1 3
# 278 "/usr/include/x86_64-linux-gnu/c++/11/bits/c++config.h" 3

# 278 "/usr/include/x86_64-linux-gnu/c++/11/bits/c++config.h" 3
namespace std
{
  typedef long unsigned int size_t;
  typedef long int ptrdiff_t;


  typedef decltype(nullptr) nullptr_t;

}
......
# 27 "/home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/101first_cmake.cpp" 2

# 27 "/home/wyr/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/101first_cmake.cpp"
using namespace std;
int main(int argc,char *argv[])
{
    cout<<"first CMake TEST"<<endl;
    return 0;
}

2.2 编译【生成*.s文件(汇编代码)】

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ cmake --build . --target  101first_cmake.s
Compiling CXX source to assembly CMakeFiles/first_cmake.dir/101first_cmake.cpp.s
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ 

在这里插入图片描述

	.file	"101first_cmake.cpp"
	.text
	.local	_ZStL8__ioinit
	.comm	_ZStL8__ioinit,1,1
	.section	.rodata
.LC0:
	.string	"first CMake TEST"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1731:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movq	%rsi, -16(%rbp)
	leaq	.LC0(%rip), %rax
	movq	%rax, %rsi
	leaq	_ZSt4cout(%rip), %rax
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rdx
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
	......
	......

2.3 汇编【生成*.o文件(二进制代码)】

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ cmake --build . --target  101first_cmake.o
Building CXX object CMakeFiles/first_cmake.dir/101first_cmake.cpp.o
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ 

在这里插入图片描述

2.4 链接【生成*.out文件(二进制代码)】

(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ cmake --build . 
Consolidate compiler generated dependencies of target first_cmake
[ 50%] Linking CXX executable first_cmake
[100%] Built target first_cmake
(base) wyr@Beyond-PC:~/cpp_study/cmake_study/1-第1章CMake快速入门篇/9fist_cmake第一个CMakeLists.txt示例/101first_cmake/build$ 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值