Naming convention for components and namespaces in cmake

假如想象 提交于 2020-05-22 05:42:26

问题


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.


回答1:


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

  • What is the naming convention for CMake scripts?
  • cmake usefulness of aliases


来源:https://stackoverflow.com/questions/48524359/naming-convention-for-components-and-namespaces-in-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!