当前位置: 首页 > 工具软件 > awesome-cmake > 使用案例 >

cmake依赖的g++版本_CPM:带有CMake的C ++很棒的依赖管理器

马欣荣
2023-12-01

cmake依赖的g++版本

C++ doesn’t have a default package manager, making the development with external libraries complex, especially if you’re making a multiplatform system. Because of this absence, the community has created some strategies to work around and there are even some pretty consolidated package managers out there, such as Conan and Vcpkg, but none of them are simple and easy to use like CPM (no, we aren’t talking about this one).

C ++没有默认的程序包管理器,这使得使用外部库进行开发变得复杂,尤其是当您要创建多平台系统时。 由于缺少这种资源,社区已经制定了一些解决方案,甚至还有一些相当整合的软件包管理器,例如ConanVcpkg ,但是它们都不像CPM那样简单易用(不,我们不是。不要谈论这个 )。

CPM is the new kid on the block, its first version was released on April 2019, it’s a CMake script that gives CMake the capacity to make version control, caching, and install the dependencies of your project, created by Lars Melchior.

CPM是新手,它的第一个版本于2019年4月发布,它是CMake脚本,使CMake能够进行版本控制,缓存和安装由Lars Melchior创建的项目的依赖项。

Usually, on a modern C++ and CMake project, the libraries are installed on the system and find_package() is called to load the library configuration, forcing every developer working on the project to have the same environment (sometimes with different tools’ versions, giving us a nasty headache).

通常,在现代C ++和CMake项目中,将库安装在系统上,并调用find_package()来加载库配置,从而迫使从事该项目的每个开发人员都具有相同的环境(有时使用不同的工具版本,我们头疼)。

CPM has a simple syntax and it’s pretty straightforward, making our lives as devs way easier. We call CPMAddPackage to install an external library (or CPMFindPackage to search the system, and, if it’s not found, install it). There are two ways to download a library: through a Github repository or a link. Let’s use Nlohmann’s JSON library as an example:

CPM具有简单的语法,而且非常简单明了,使我们的开发人员生活更加轻松。 我们调用CPMAddPackage来安装一个外部库(或CPMFindPackage来搜索系统,如果找不到它,则安装它)。 有两种下载库的方法:通过Github存储库或链接。 让我们以Nlohmann的JSON库为例:

Or through a link:

或通过链接:

add_library() and add_library()target_include_directories() if you aren’t using the Github feature). target_include_directories() )。

All right, to understand the simplicity and ease of CPM let’s create a small project and do a quick example using spdlog, made by Gabi Melman.This is how the source tree will look like:

好吧,为了理解CPM的简单性和易用性,让我们创建一个小项目并使用Gabi Melman制作的spdlog做一个快速示例,这就是源代码树的样子:

src
|___main.cpp
CMakeLists.txt

This is our CMakeLists.txt:

这是我们的CMakeLists.txt:

First, we have to download CPM’s script on our project and include it on CMake. There are two ways to do this. The first one is manually downloading it with the following commands:

首先,我们必须在项目上下载CPM脚本并将其包含在CMake中。 有两种方法可以做到这一点。 第一个是使用以下命令手动下载它:

$ mkdir -p cmake$ wget -O cmake/CPM.cmake https://github.com/TheLartians/CPM.cmake/releases/latest/download/CPM.cmake

Then adding the line include(cmake/CPM.cmake) on CMakeLists.txt.

然后在CMakeLists.txt上添加include(cmake / CPM.cmake)行。

The second way is adding the following snippet to CMake, which will check if CPM exists everytime the project builds (and download it if it doesn’t exist):

第二种方法是将以下代码段添加到CMake,该代码段将在每次构建项目时检查CPM是否存在(如果不存在,则下载CPM):

Then, our source tree will look like this:

然后,我们的源代码树将如下所示:

build
|___cmake
|___CPM.cmake
src
|___main.cpp
CmakeListst.txt

To implement spdlog to our project, just add the following snippet to CMakeLists.txt, after including CPM:

要将spdlog实施到我们的项目,只需在包含CPM之后将以下代码段添加到CMakeLists.txt中即可:

Then we have to link the library to the project:

然后,我们必须将库链接到项目:

In the end, our CMakeLists.txt should look like this:

最后,我们的CMakeLists.txt应该如下所示:

Now in our main, let’s use spdlog to output a “Hello World!”:

现在,在我们的主目录中,让我们使用spdlog输出“ Hello World!”:

And that’s it, now we have to build the project and CPM will do all the hard work of downloading and showing CMake the path of our dependencies. So let’s create a build directory, build the project, and run it:

就是这样,现在我们必须构建项目,CPM将完成所有下载和显示CMake依赖关系路径的艰苦工作。 因此,让我们创建一个构建目录,构建项目并运行它:

$ mkdir -p build$ cd build$ cmake ..$ make -j4$ ./cpm_example

Since CPM is a dependency manager and not a package manager, if you’re working on multiple projects that use the same dependencies, you can easily set up different versions of the same library.

由于CPM是依赖项管理器而不是包管理器,因此,如果您正在处理使用相同依赖项的多个项目,则可以轻松设置同一库的不同版本。

The only downside of CPM is that every time you run a git clean -fdx or delete the build folder, it will remove the dependencies directory, and you will have to download everything again the next time you run cmake. But CPM lets you use a cache directory, passing the parameter -DCPM_SOURCE_CACHE to CMake and specifying a path. Like this:

CPM的唯一缺点是,每次您运行git clean -fdx或删除build文件夹时,它将删除依赖关系目录,并且下次运行cmake时必须再次下载所有内容。 但是CPM允许您使用缓存目录,将-DCPM_SOURCE_CACHE参数传递给CMake并指定路径。 像这样:

$ cmake -DCPM_SOURCE_CACHE=/tmp/deps ..

With just a few lines of CMake code, we can manage our external dependencies of a project, making environment configuration so much easier. CPM gives you a great advantage if you are working with a multiplatform project, CI tools (such as Travis and Appveyor), building inside a container, or any other situation where you have to set up a new environment from zero.

仅需几行CMake代码,我们就可以管理项目的外部依赖关系,从而简化了环境配置。 如果您正在处理多平台项目,CI工具(例如TravisAppveyor ),在容器内进行构建或任何其他必须从零开始建立新环境的情况,则CPM会为您带来巨大的优势。

翻译自: https://medium.com/swlh/cpm-an-awesome-dependency-manager-for-c-with-cmake-3c53f4376766

cmake依赖的g++版本

 类似资料: