Cmake基础(2)

使用一个简单的示例来应用cmake,无任何三方库的单一的应用程序项目

你可以收获

  • 使用cmake生成VS项目
  • 生成mingw项目(makefile)

1 首先新建一个cpp,我们要做一个控制台应用程序

#include<iostream>
void main(){
   
	std::cout<<"hello cmake\n";
}

下面是CMakeLists.txt,要和main.cpp放在同一目录下
当你只有一个文件 main.cpp 并想要生成一个可执行程序时,你的 CMakeLists.txt 文件可以非常简单。以下是一个示例:

# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)

# 项目名称
project(MyExecutable)

# 添加可执行文件
add_executable(my_executable main.cpp)

这个 CMakeLists.txt 文件包含了三个主要部分:

  1. cmake_minimum_required(VERSION 3.12):指定 CMake 的最低版本要求。这个版本号可以根据你的需求进行修改。

  2. project(MyExecutable):定义项目名称。在这里,项目名被设置为 “MyExecutable”,你可以根据需要修改。

  3. add_executable(my_executable main.cpp):添加可执行文件。这一行告诉 CMake 创建一个名为 my_executable 的可执行文件,它的源代码是 main.cpp。确保 main.cpp 文件与 CMakeLists.txt 文件在同一目录下,或者根据实际情况提供正确的路径。

2 在项目目录中创建一个 build 目录,并在其中运行 CMake 和构建命令:

mkdir build
cd build
cmake ..
cmake --build .

这样就会在 build 目录中生成一个名为 my_executable 的可执行文件。
cmake …:配置项目,生成构建系统文件。

cmake --build .:使用生成的构建系统文件实际构建项目。这个可不用执行,因为VS提供可视化的编译。只用打开生成的sln文件,再使用VSbuild。

执行之后

PS E:\workspace\cmake_demo\simple_demo\build> cmake ..
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22631.
-- The C compiler identification is MSVC 19.29.30153.0
-- The CXX compiler identification is MSVC 19.29.30153.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: E:/VS/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - 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: E:/VS/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (6.3s)
-- Generating done (0.0s)
-- Build files have been written to: E:/workspace/cmake_demo/simple_demo/build
PS E:\workspace\cmake_demo\simple_demo\build> cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版权所有(C) Microsoft Corporation。保留所有权利。

  my_executable.vcxproj -> E:\workspace\cmake_demo\simple_demo\build\Debug\my_executable.exe
  Building Custom Rule E:/workspace/cmake_demo/simple_demo/CMakeLists.txt
PS E:\workspace\cmake_demo\simple_demo\build>

build 目录下新增了如下文件:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2023/12/16      2:50                CMakeFiles
d-----        2023/12/16      2:44                Debug
d-----        2023/12/16      2:44                my_executable.dir
d-----        2023/12/16      2:44                x64
-a----        2023/12/16      2:42          38740 ALL_BUILD.vcxproj
-a----        2023/12/16      2:42            290 ALL_BUILD.vcxproj.filters
-a----        2023/12/16      2:44            168 ALL_BUILD.vcxproj.user
-a----        2023/12/16      2:42          13988 CMakeCache.txt
-a----        2023/12/16      2:42           1459 cmake_install.cmake
-a----        2023/12/16      2:42           3126 MyExecutable.sln
-a----        2023/12/16      2:42          48205 my_executable.vcxproj
-a----        2023/12/16      2:42            583 my_executable.vcxproj.filters
-a----        2023/12/16      2:44            168 my_executable.vcxproj.user
-a----        2023/12/16      2:42          38830 ZERO_CHECK.vcxproj
-a----        2023/12/16      2:42            533 ZERO_CHECK.vcxproj.filters

其中ALL_BUILD和ZERO_CHECK是cmake附加的,一个是可以编译所有项目的项目,一个是重新生成VS项目

3 刚刚生成了VS的文件,现在生成makefile文件

使用-G命令来指定编译器
-G <generator-name> = Specify a build system generator.
使用cmake -h来查看帮助,看看编译器的名字

Generators

The following generators are available on this platform (* marks default):
  Visual Studio 17 2022        = Generates Visual Studio 2022 project files.
                                 Use -A option to specify architecture.
* Visual Studio 16 2019        = Generates Visual Studio 2019 project files.
                                 Use -A option to specify architecture.
  MinGW Makefiles              = Generates a make file for use with
                                 mingw32-make.
(多余的我就删去了,不然太长)

以下是一个在 MinGW 下使用 Makefile 的示例:

mkdir build_mingw
cd build_mingw
cmake -G "MinGW Makefiles" ..

执行之后:

PS E:\workspace\cmake_demo\simple_demo\build_mingw> cmake -G "MinGW Makefiles" ..
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: E:/MinGW/mingw64/bin/gcc.exe - 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: E:/MinGW/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.4s)
-- Generating done (0.0s)
-- Build files have been written to: E:/workspace/cmake_demo/simple_demo/build_mingw

至此以及生成了makefile

然后,你可以使用 mingw32-make 命令来构建项目:
生成了可执行文件

[ 50%] Building CXX object CMakeFiles/my_executable.dir/main.cpp.obj
[100%] Linking CXX executable my_executable.exe
[100%] Built target my_executable

在makefile的环境下因为没有IDE可视化的支持,因此一般将cmake 和make(mingw32-make)连用,在Cmake生成makefile时就指定64位还是32位,debug还是release。
cmake -G "MinGW Makefiles" -DCMAKE_C_FLAGS=-m64 -DCMAKE_BUILD_TYPE=Release ..
64可以换成32,Release 可以换成Debug

后面的文章中在讨论项目配置的问题,这篇文章主要是体会一下cmake生成和编译的步骤

相关推荐

  1. Cmake基础(2)

    2023-12-17 02:20:06       32 阅读
  2. Cmake基础(6)

    2023-12-17 02:20:06       34 阅读
  3. Cmake基础(5)

    2023-12-17 02:20:06       46 阅读
  4. Cmake基础(7)

    2023-12-17 02:20:06       32 阅读
  5. cmake学习笔记2

    2023-12-17 02:20:06       14 阅读
  6. CMake基本使用

    2023-12-17 02:20:06       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-17 02:20:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-17 02:20:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-17 02:20:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-17 02:20:06       20 阅读

热门阅读

  1. 网格布局 Grid

    2023-12-17 02:20:06       44 阅读
  2. ES的字段更改字段类型

    2023-12-17 02:20:06       37 阅读
  3. springBoot使用threadPoolTaskExecutor多线程

    2023-12-17 02:20:06       36 阅读
  4. spring 笔记二 spring配置数据源和整合测试功能

    2023-12-17 02:20:06       31 阅读
  5. Springboot Minio最新版大文件下载

    2023-12-17 02:20:06       45 阅读
  6. C 标准库 - <string.h>

    2023-12-17 02:20:06       32 阅读
  7. echarts 柱形图、折线图点击事件

    2023-12-17 02:20:06       34 阅读
  8. Docker笔记:简单部署 nodejs 项目和 golang 项目

    2023-12-17 02:20:06       34 阅读
  9. Python中的名称空间和作用域

    2023-12-17 02:20:06       38 阅读
  10. NLP中的Seq2Seq与attention注意力机制

    2023-12-17 02:20:06       38 阅读
  11. Unity项目里Log系统该怎么设计

    2023-12-17 02:20:06       30 阅读
  12. docker配置容器内访问主机服务

    2023-12-17 02:20:06       40 阅读
  13. axios+echarts

    2023-12-17 02:20:06       30 阅读