Linux cmake 命令实战示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将探索 Linux 的 cmake 命令及其实际应用。我们将从在 Ubuntu 22.04 环境中安装 CMake 开始,CMake 是一个流行的构建自动化工具。然后,我们将创建一个简单的 C++ 项目,并学习如何使用 CMake 来管理构建过程。最后,我们将了解 CMake 中可用的不同构建配置。

本实验涵盖的步骤对于使用 C/C++ 项目并需要高效管理复杂构建过程的开发人员至关重要。通过本实验,你将深入了解如何使用 CMake 来简化你的开发工作流程。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") subgraph Lab Skills linux/echo -.-> lab-422608{{"`Linux cmake 命令实战示例`"}} linux/sudo -.-> lab-422608{{"`Linux cmake 命令实战示例`"}} linux/apt -.-> lab-422608{{"`Linux cmake 命令实战示例`"}} end

在 Ubuntu 22.04 上安装 CMake

在这一步骤中,我们将在 Ubuntu 22.04 环境中安装 CMake,这是一个流行的构建自动化工具。

首先,让我们更新包索引:

sudo apt-get update

示例输出:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

接下来,安装 CMake 所需的必要包:

sudo apt update
sudo apt-get install -y cmake

示例输出:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  cmake-data libarchive13 libjsoncpp24 librhash0 libssl-dev libssl1.1
Suggested packages:
  cmake-doc
The following NEW packages will be installed:
  cmake cmake-data libarchive13 libjsoncpp24 librhash0 libssl-dev libssl1.1
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 15.6 MB of archives.
After this operation, 67.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libssl1.1 amd64 1.1.1m-3ubuntu1 [1,296 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 libssl-dev amd64 1.1.1m-3ubuntu1 [1,547 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libarchive13 amd64 3.5.2-2 [262 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 libjsoncpp24 amd64 1.9.5-2 [104 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 librhash0 amd64 1.4.2-1 [106 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/universe amd64 cmake-data all 3.22.1-1ubuntu1 [1,605 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy/universe amd64 cmake amd64 3.22.1-1ubuntu1 [10.7 MB]
Fetched 15.6 MB in 3s (5,204 kB/s)
Selecting previously unselected package libssl1.1:amd64.
(Reading database ... 78852 files and directories currently installed.)
Preparing to unpack .../libssl1.1_1.1.1m-3ubuntu1_amd64.deb ...
Unpacking libssl1.1:amd64 (1.1.1m-3ubuntu1) ...
Selecting previously unselected package libssl-dev:amd64.
Preparing to unpack .../libssl-dev_1.1.1m-3ubuntu1_amd64.deb ...
Unpacking libssl-dev:amd64 (1.1.1m-3ubuntu1) ...
Selecting previously unselected package libarchive13:amd64.
Preparing to unpack .../libarchive13_3.5.2-2_amd64.deb ...
Unpacking libarchive13:amd64 (3.5.2-2) ...
Selecting previously unselected package libjsoncpp24:amd64.
Preparing to unpack .../libjsoncpp24_1.9.5-2_amd64.deb ...
Unpacking libjsoncpp24:amd64 (1.9.5-2) ...
Selecting previously unselected package librhash0:amd64.
Preparing to unpack .../librhash0_1.4.2-1_amd64.deb ...
Unpacking librhash0:amd64 (1.4.2-1) ...
Selecting previously unselected package cmake-data.
Preparing to unpack .../cmake-data_3.22.1-1ubuntu1_all.deb ...
Unpacking cmake-data (3.22.1-1ubuntu1) ...
Selecting previously unselected package cmake.
Preparing to unpack .../cmake_3.22.1-1ubuntu1_amd64.deb ...
Unpacking cmake (3.22.1-1ubuntu1) ...
Setting up libssl1.1:amd64 (1.1.1m-3ubuntu1) ...
Setting up libssl-dev:amd64 (1.1.1m-3ubuntu1) ...
Setting up libarchive13:amd64 (3.5.2-2) ...
Setting up libjsoncpp24:amd64 (1.9.5-2) ...
Setting up librhash0:amd64 (1.4.2-1) ...
Setting up cmake-data (3.22.1-1ubuntu1) ...
Setting up cmake (3.22.1-1ubuntu1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3) ...

安装现已完成。你可以通过运行以下命令来验证 CMake 版本:

cmake --version

示例输出:

cmake version 3.22.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

太棒了!我们已成功在 Ubuntu 22.04 环境中安装了 CMake。

使用 CMake 创建一个简单的 C++ 项目

在这一步骤中,我们将创建一个简单的 C++ 项目,并使用 CMake 来构建它。

首先,让我们为项目创建一个新目录:

cd ~/project
mkdir cpp-project && cd cpp-project

接下来,创建一个名为 main.cpp 的 C++ 文件,内容如下:

#include <iostream>

int main() {
    std::cout << "Hello, CMake!" << std::endl;
    return 0;
}

现在,我们需要在同一目录下创建一个 CMakeLists.txt 文件,以定义项目的构建过程。将以下内容添加到文件中:

cmake_minimum_required(VERSION 3.22)
project(cpp-project)

add_executable(cpp-project main.cpp)

这个 CMake 文件设置了所需的最低 CMake 版本,定义了项目名称,并添加了一个名为 cpp-project 的可执行目标,该目标将从 main.cpp 文件构建。

要构建项目,请运行以下命令:

cmake -S . -B build
cmake --build build

第一条命令在 build 目录中生成构建文件,第二条命令实际构建项目。

示例输出:

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.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/labex/project/cpp-project/build
Scanning dependencies of target cpp-project
[ 50%] Building CXX object CMakeFiles/cpp-project.dir/main.cpp.o
[100%] Linking CXX executable cpp-project
[100%] Built target cpp-project

最后,运行构建的可执行文件:

./build/cpp-project

示例输出:

Hello, CMake!

恭喜!你已成功创建了一个简单的 C++ 项目,并使用 CMake 构建了它。

理解 CMake 构建配置

在这一步骤中,我们将探索 CMake 中可用的不同构建配置以及如何使用它们。

CMake 支持多种构建配置,例如 DebugReleaseRelWithDebInfoMinSizeRel。这些配置决定了构建过程中使用的优化和调试设置。

让我们从创建一个新的 C++ 项目开始:

cd ~/project
mkdir cmake-configurations && cd cmake-configurations

现在,创建一个 main.cpp 文件,内容如下:

#include <iostream>

int main() {
    std::cout << "Build Configuration: " << CMAKE_BUILD_TYPE << std::endl;
    return 0;
}

接下来,创建一个 CMakeLists.txt 文件,内容如下:

cmake_minimum_required(VERSION 3.22)
project(cmake-configurations)

add_executable(cmake-configurations main.cpp)

要使用默认的 Debug 配置构建项目,请运行以下命令:

cmake -S . -B build
cmake --build build

示例输出:

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.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/labex/project/cmake-configurations/build
Scanning dependencies of target cmake-configurations
[ 50%] Building CXX object CMakeFiles/cmake-configurations.dir/main.cpp.o
[100%] Linking CXX executable cmake-configurations
[100%] Built target cmake-configurations

运行构建的可执行文件:

./build/cmake-configurations

示例输出:

Build Configuration: Debug

现在,让我们使用 Release 配置构建项目:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

示例输出:

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.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/labex/project/cmake-configurations/build
Scanning dependencies of target cmake-configurations
[ 50%] Building CXX object CMakeFiles/cmake-configurations.dir/main.cpp.o
[100%] Linking CXX executable cmake-configurations
[100%] Built target cmake-configurations

运行构建的可执行文件:

./build/cmake-configurations

示例输出:

Build Configuration: Release

如你所见,构建配置反映在可执行文件的输出中。默认情况下使用 Debug 配置,但你可以在运行 cmake 时使用 -DCMAKE_BUILD_TYPE 选项指定不同的配置。

总结

在本实验中,我们首先在 Ubuntu 22.04 环境中安装了 CMake,这是一个流行的构建自动化工具。我们更新了包索引,然后安装了 CMake 所需的必要包。接下来,我们创建了一个简单的 C++ 项目,并学习了如何使用 CMake 来构建和管理项目。我们还探索了不同的 CMake 构建配置,这些配置使我们能够为不同的环境或目的自定义构建过程。

Linux 命令速查表

您可能感兴趣的其他 Linux 教程