cmake 中组件和命名空间的命名约定
Naming convention for components and namespaces in cmake
简而言之:
cmake 库目标是否有任何首选命名约定 - 特别是在使用名称空间时?
注:
除非真的有 objective 原因,否则我不是在问个人喜好,而是问是否有 "official"(例如 kitware 推荐)或已建立(可能偏离)惯例。
详情:
假设我有一个 library/framework foo
,它有单独的组件 bar
和 baz
。到目前为止,我的命名约定如下所示:
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)
(当然问题也延伸到export sets,但我不想把问题复杂化)
然而,我无法真正找到的是,这些目标是否有首选的甚至官方的命名约定。
我看过的东西:
- 命名空间部分:
- 有些项目好像首字母大写,有些不是(前者好像比较常见)
- 组成部分:
- 在某些项目中,组件名称与二进制名称相同
- 带或不带 "lib" 前缀(libfoo-bar 与 foo-bar)
- 有或没有命名空间(foo-bar 与 bar)
- 有些项目首字母大写
- 一些项目使用 CamelCase 一些 snake_case,即使二进制文件或项目名称不遵循这些约定。
我想主要的问题是一般的库没有命名约定,所以很难在 CMake 中提出命名约定,但至少命名空间的首字母大写和组件似乎很常见,所以我想知道是否有一些我应该在未来的项目中遵循的指南。
cmake-developer
documentation 给出了以下关于命名空间的建议:
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
).
而CMP0028
policy documentation在"common pattern"上表示在命名空间的使用中:
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.
不,库目标的命名没有 CMake 特定约定。但是由于名称默认作为目标的输出名称:
- 我更喜欢为目标使用与我的源代码目录相同的名称
- 并且不添加
lib
前缀,因为这是由 CMake 根据您编译项目所用的平台自动添加的
来自 CMake 教程
您可以获得的最官方来源可能是来自 Kitware 的 "Mastering CMake" book 作者 Ken Martin 和 Bill Hoffman 撰写的摘录。
tutorials from the book 全部使用 CamelCase,component/target 名称没有名称空间。
参考文献
简而言之:
cmake 库目标是否有任何首选命名约定 - 特别是在使用名称空间时?
注:
除非真的有 objective 原因,否则我不是在问个人喜好,而是问是否有 "official"(例如 kitware 推荐)或已建立(可能偏离)惯例。
详情:
假设我有一个 library/framework foo
,它有单独的组件 bar
和 baz
。到目前为止,我的命名约定如下所示:
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)
(当然问题也延伸到export sets,但我不想把问题复杂化)
然而,我无法真正找到的是,这些目标是否有首选的甚至官方的命名约定。
我看过的东西:
- 命名空间部分:
- 有些项目好像首字母大写,有些不是(前者好像比较常见)
- 组成部分:
- 在某些项目中,组件名称与二进制名称相同
- 带或不带 "lib" 前缀(libfoo-bar 与 foo-bar)
- 有或没有命名空间(foo-bar 与 bar)
- 有些项目首字母大写
- 一些项目使用 CamelCase 一些 snake_case,即使二进制文件或项目名称不遵循这些约定。
我想主要的问题是一般的库没有命名约定,所以很难在 CMake 中提出命名约定,但至少命名空间的首字母大写和组件似乎很常见,所以我想知道是否有一些我应该在未来的项目中遵循的指南。
cmake-developer
documentation 给出了以下关于命名空间的建议:
When providing imported targets, these should be namespaced (hence the
Foo::
prefix); CMake will recognize that values passed totarget_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 policyCMP0028
).
而CMP0028
policy documentation在"common pattern"上表示在命名空间的使用中:
The use of double-colons is a common pattern used to namespace
IMPORTED
targets andALIAS
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.
不,库目标的命名没有 CMake 特定约定。但是由于名称默认作为目标的输出名称:
- 我更喜欢为目标使用与我的源代码目录相同的名称
- 并且不添加
lib
前缀,因为这是由 CMake 根据您编译项目所用的平台自动添加的
来自 CMake 教程
您可以获得的最官方来源可能是来自 Kitware 的 "Mastering CMake" book 作者 Ken Martin 和 Bill Hoffman 撰写的摘录。
tutorials from the book 全部使用 CamelCase,component/target 名称没有名称空间。
参考文献