CMake编译工程/第一个CMakeLists.txt(最详细案例演示)

目录

在 linux 平台下使用 CMake 构建C/C++工程的流程:

当前项目目录结构

最小CMake工程

进入文件夹5.3.1,VScode打开项目文件5.3.1

在项目5.3.1顶层目录中,New File一个CMakeLists.txt

 编写CMakeLists.txt

编译CMakeLists.txt(两种方式)

内部构建(不建议使用)

外部构建(推荐使用)

 多目录文件工程CMake编译

当前工程文件5.3.2目录结构

在顶层目录5.3.2单击右键,New File一个CMakelIsts.txt

编写文件CMakelIsts.txt


在 linux 平台下使用 CMake 构建C/C++工程的流程:

  • 手动编写 CmakeLists.txt
  • 执行命令 cmake PATH 生成 Makefile ( PATH 是顶层CMakeLists.txt 所在的目录 )。
  • 执行命令 make 进行编译。

当前项目目录结构

有两个项目5.3.1和5.3.2

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第1张图片

最小CMake工程

进入文件夹5.3.1,VScode打开项目文件5.3.1

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第2张图片

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第3张图片

在项目5.3.1顶层目录中,New File一个CMakeLists.txt

 CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第4张图片

 编写CMakeLists.txt

cmake_minimum_required(VERSION 3.0)     #指定 cmake最小版本要求为3.0

 project(HELLOWORLD)  #指定项目名称,习惯用大写指定项目名称

 #等价于g++  helloworld.cpp -o helloworld;helloworld_cmake帮助区分以前生成的helloword
 #第一个参数指定生成可执行文件名称helloworld_cmake,第二参数要编译的源文件helloworld.cpp
add_executable(helloworld_cmake helloworld.cpp)

 注意:CMakeLists.txt文件的注释是用#

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第5张图片

可在终端查看当前安装的camke版本信息

编译CMakeLists.txt(两种方式)

内部构建(不建议使用)

内部构建会在同级目录下产生一大堆中间文件,这些中间文件并不是我们最终所需要的,和
工程源文件放在一起会显得杂乱无章。流程如下:

 内部构建——直接在当前文件夹下编译CMakeLists.txt;
1 在当前目录下,编译本目录的CMakeLists.txt,生成Makefile和其他文件
2 cmake .
3  执行make命令,生成target
4 make

编译前文件夹目录如下:

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第6张图片

ctrl+`打开终端编译CMakeLists.txt,cmake . 表示对当前目录下的CMakeLists.txt进行编译;

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第7张图片

编译信息解释:

HP-virtual-machine:~/c++/5.3/5.3.1$ cmake .
//下列至Detecting结束,在检测c++编译器
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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/hkm/c++/5.3/5.3.1  //说明新建文件被写到了/home/hkm/c++/5.3/5.3.1路径文件夹下

编译后文件夹目录显示如下:(发现多了很多中间文件,编译前只有3个文件)

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第8张图片

 对Makefile文件进行make;

 编译信息解释:

HP-virtual-machine:~/c++/5.3/5.3.1$ make
Scanning dependencies of target helloworld_cmake
[ 50%] Building CXX object CMakeFiles/helloworld_cmake.dir/helloworld.cpp.o //正在构建cpp.o文件
[100%] Linking CXX executable helloworld_cmake //链接可执行文件helloworld_cmake
[100%] Built target helloworld_cmake  //生成target,target名字叫helloworld_cmake

 在文件夹目录下可以看到生成的helloworld_cmake

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第9张图片

查看存在两个可执行文件;

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第10张图片

执行helloworld_cmake文件;

外部构建(推荐使用)

将编译输出文件与源文件放到不同目录中;流程如下:

 外部构建
1. 在当前目录下,创建build文件夹
 mkdir build
2. 进入到build文件夹
 cd build
3. 编译上级目录的CMakeLists.txt,生成Makefile和其他文件
 cmake ..
4. 执行make命令,生成target
make

删除内部构建生成的中间文件,仅保留构建前的3个文件;

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第11张图片

 CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第12张图片

第一步:在当前目录下,创建build文件夹并进入build

 CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第13张图片

编译CMakeLists.txt,由于现在终端在build文件夹下,CMakeLists.txt在当前路径的上一级目录,因此是cmake . .

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第14张图片

 执行make命令;

 此时查看,此次生成的中间文件,全部放在build文件下,文件夹目录不再看起来混乱

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第15张图片

 点击展开build文件夹显示如下:

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第16张图片

执行build文件夹下生成的可执行文件./helloworld_cmake

 多目录文件工程CMake编译

当前工程文件5.3.2目录结构

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第17张图片

在顶层目录5.3.2单击右键,New File一个CMakelIsts.txt

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第18张图片

编写文件CMakelIsts.txt

cmake_minimum_required(VERSION 3.0)

project(SWAP)

#等价于g++ main.cpp src/swap.cpp -Iinclude -o  main中的-Iinclude头文件搜索路径
include_directories(include)
#等价于g++ main.cpp src/swap.cpp -Iinclude -o  main_cmake
add_executable(main_cmake main.cpp src/swap.cpp)

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第19张图片

 在当前目录下,创建build文件夹 mkdir build;进入到build文件夹 cd build

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第20张图片

  编译上级目录的CMakeLists.txt,生成Makefile和其他文件 cmake ..

 CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第21张图片

执行make命令;生成target文件

CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第22张图片

 执行生成的可执行文件main_cmake; 可见,无论用cmake还是g++进行构建,效果一致;CMake编译工程/第一个CMakeLists.txt(最详细案例演示)_第23张图片

 

你可能感兴趣的:(C++,linux,c++,linux,vscode)