# 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$