在CMake中自动使用目录作为项目名称 [英] Automatically use the directory as the project name in CMake

查看:355
本文介绍了在CMake中自动使用目录作为项目名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用CMake来管理构建系统方面还很陌生,所以如果我很愚蠢,这是一个坏主意,请告诉我。

I'm fairly new at using CMake to manage my build system, so if I'm being stupid and this is a bad idea, please let me know.

我希望能够设置cmakelists.txt文件,以便在执行此操作

I'd like to be able to set up my cmakelists.txt file so that when I do

project( ... )

目录名称将自动成为项目名称。我想这样做是因为我发现能够将一个项目的整个目录复制为另一个项目的起点很方便。但是,尽管我总是将目录重命名为有意义的名称,但我常常忘记更改cmakelists.txt文件的 project(name)行,然后最后得到多个

the name of the directory becomes the project name automatically. I want to do this because I find it convenient to be able to copy the entire directory of one project as the starting point of another. However, though I always rename the directory to something meaningful, I often forget to change the project(name) line of the cmakelists.txt file, and then I end up with multiple projects open in my build environment with the same name, which gets confusing.

理想情况下,如果目录名称中有空格,则将它们用下划线替换。

Ideally, if there are spaces in the directory name they'd be replaced by underscores.

p>

CMake可以这样做吗?

Can CMake do this? And is it a bad idea for some reason I'm not seeing?

推荐答案

您可以通过以下方法实现此目的: CMakeLists.txt的开头:

You can achieve this by adding the following to the start of your CMakeLists.txt:

get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId})

我不尽管我认为生产项目通常会有一个预定义的名称,该名称可以在 project 命令中显式设置,但对于一次性项目而言,这样做没有问题。

I don't see a problem with doing this for throwaway projects, although I guess production projects would normally have a predefined name which would be set explicitly in the project command.

当您提到将一个项目的整个目录复制为另一个项目的起点时,我假设您在复制时不包括构建树吗? CMake 确实无法处理正在移动的构建树

When you mention that you "copy the entire directory of one project as the starting point of another", I assume you don't include the build tree when you copy? CMake isn't really able to handle the build tree being moved.

这篇关于在CMake中自动使用目录作为项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆