cmake中组件和名称空间的命名约定 [英] Naming convention for components and namespaces in cmake

查看:448
本文介绍了cmake中组件和名称空间的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:

对于cmake库目标,是否有任何首选的命名约定-特别是在使用名称空间时?

注意:

除非确实有客观原因,否则我不是在问个人喜好,而是要问是否存在正式"(例如,由kitware推荐)或已建立(可能会有所不同)的惯例.

详细信息:

可以说我有一个库/框架foo,其中包含单独的组件barbaz.到目前为止,我的命名约定如下:

add_library(foo-bar src1.cpp, scr2.cpp)
add_library(foo-baz src3.cpp, src4.cpp)

现在,我想使用命名空间(::)约定添加别名目标.例如

add_library(Foo::Bar ALIAS foo-bar)
add_library(Foo::Baz ALIAS foo-baz)

(当然问题也扩展到导出集,但我不想使问题复杂化)
但是,我真正无法发现的是这些目标是否有首选甚至正式的命名约定.

我所见:

  • 命名空间部分:
    • 有些项目似乎首字母大写,有些则不大(前者似乎更常见)
  • 组成部分:
    • 在某些项目中,组件名称与二进制名称相同
    • 带有或不带有"lib"前缀(libfoo-bar与foo-bar)
    • 有或没有命名空间(foo-bar vs bar)
    • 一些项目的首字母大写
    • 即使二进制文件或项目名称未遵循这些约定,某些项目也使用CamelCase进行snake_case的操作.

我猜主要的问题是,通常没有库的命名约定,因此很难在CMake中提出命名约定,但是至少名称空间的第一个字母和组件的首字母大写化似乎很常见,所以我想知道将来的项目是否应该遵循一些准则.

解决方案

CMP0028政策文档在命名空间中使用通用模式":

使用双冒号是用于命名空间IMPORTED目标和ALIAS目标的常见模式.在计算目标的链接依赖项时,每个依赖项的名称可以是目标,也可以是磁盘上的文件.以前,如果找不到与名称匹配的目标,则该名称被认为是指磁盘上的文件.如果目标名称应有错字,则可能导致混淆错误消息.

不,对于库目标的命名,没有特定于CMake的约定.但是由于默认情况下使用该名称作为目标的输出名称:

  • 我希望目标名称与源代码目录的名称相同
  • 并且不添加任何lib前缀,因为CMake会自动添加该前缀,具体取决于您使用的编译项目的平台

来自CMake教程

您可以获得的最正式的资源可能是掌握CMake"的摘录. 由Kitware的Ken Martin和Bill Hoffman撰写.

本书的教程都使用CamelCase,并且没有名称空间作为组件/目标名称. /p>

参考

In Short:

Is there any preferred naming convention for cmake library targets - in particular when using namespaces?

Note:

Unless there is really an objective reason for it, I'm not asking about personal preferences, but whether there is either an "official" (e.g. recommended by kitware) or established (which might deviate) convention.

Details:

Lets say I have a library/framework foo which has the individual components bar and baz. So far, my naming convention looks like this:

add_library(foo-bar src1.cpp, scr2.cpp)
add_library(foo-baz src3.cpp, src4.cpp)

Now I want to add alias targets using the namespace (::) convention. E.g.

add_library(Foo::Bar ALIAS foo-bar)
add_library(Foo::Baz ALIAS foo-baz)

(Of course the question also extends to export sets, but I didn't want to complicate the question)
What I couldn't really find out however, is if there is a preferred or even official naming convention for those targets.

Things I've seen:

  • Namespace part:
    • some projects seem to capitalize the first letter, some not (the former seems to be more common)
  • Component part:
    • in some projects, the component name is the same as the binary name
    • with or without the "lib" prefix (libfoo-bar vs foo-bar)
    • with or without the namespace (foo-bar vs bar)
    • some projects capitalize the first letter
    • some projects use CamelCase some snake_case, even if the binaries or project names don't follow those conventions.

I guess the main problem is that there is no naming convention for libraries in general so that makes it hard to come up with a naming convention in CMake, but at least the capitilization for the first letter of the namespace and the component seem to be pretty common, so I was wondering if there is some guideline I should follow for future projects.

解决方案

The cmake-developer documentation gives the following advice on namespaces:

When providing imported targets, these should be namespaced (hence the Foo:: prefix); CMake will recognize that values passed to target_link_libraries() that contain :: in their name are supposed to be imported targets (rather than just library names), and will produce appropriate diagnostic messages if that target does not exist (see policy CMP0028).

And the CMP0028 policy documentation says on the "common pattern" in the use of namespaces:

The use of double-colons is a common pattern used to namespace IMPORTED targets and ALIAS targets. When computing the link dependencies of a target, the name of each dependency could either be a target, or a file on disk. Previously, if a target was not found with a matching name, the name was considered to refer to a file on disk. This can lead to confusing error messages if there is a typo in what should be a target name.

And no, there are no CMake specific convention for the naming of library targets. But since the name is taken by default as the target's output name:

  • I prefer to take the same name for the targets as for my source code directory
  • And add no lib prefix, since this is automatically added by CMake depending on the platform you are compiling your project with

From the CMake Tutorial

The most official source you could get is probably an extract from the "Mastering CMake" book written by Ken Martin and Bill Hoffman from Kitware.

The tutorials from the book all use CamelCase and no namespaces for component/target names.

References

这篇关于cmake中组件和名称空间的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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