CMake库目标与命名空间的官方/通用命名规范咨询
Great question! When it comes to CMake library target naming—especially with namespaces—there are official recommendations from Kitware (the maintainers of CMake) as well as widely adopted community conventions, rather than just personal preference. Let’s break this down clearly:
Official Kitware Recommendations
Kitware’s official documentation and best practice guides outline specific rules for target naming, particularly for aliases and namespaces:
- Namespace portion: Use PascalCase (initial capitalization) for the namespace (e.g.,
Foo::instead offoo::). This aligns with CMake’s own internal style and matches how most well-known third-party libraries (like Qt, Boost) expose their targets. - Alias target components: The component part of the alias should mirror your library’s logical component name, also using initial capitalization (e.g.,
Foo::Barfor thebarcomponent). This creates a clear, intuitive mapping between logical components and their CMake targets. - The underlying "real" library targets (the ones you create directly with
add_library) can still follow system-friendly snake_case or kebab-case (e.g.,foo-bar), since these correspond to actual library filenames (likelibfoo-bar.soon Linux orfoo-bar.dllon Windows) which often prefer lowercase, non-camelcase names.
Widely Adopted Community Conventions
Beyond official guidance, the CMake community has settled on consistent patterns that most mature projects follow:
- Real library targets: Stick to lowercase, kebab-case (
foo-bar) or snake_case (foo_bar) to avoid cross-platform filename issues (some filesystems are case-sensitive, others aren’t). - Namespace aliases: Always use the
Namespace::Componentformat, where both the namespace and component are capitalized. This makes target references instantly recognizable (e.g.,target_link_libraries(my_app PRIVATE Foo::Bar Foo::Baz)reads like plain English). - Export sets (though you mentioned you’re focusing on aliases for now) typically follow a similar pattern, e.g.,
FooTargetsfor the exported target collection.
Specific Recommendation for Your Scenario
Given your foo framework with bar and baz components, here’s the recommended implementation that aligns with both official and community standards:
# Real library targets: system-friendly kebab-case add_library(foo-bar src1.cpp src2.cpp) add_library(foo-baz src3.cpp src4.cpp) # Alias targets: official PascalCase namespace + component add_library(Foo::Bar ALIAS foo-bar) add_library(Foo::Baz ALIAS foo-baz)
This approach gives you the best of both worlds:
- The real library filenames follow platform conventions, avoiding compatibility headaches.
- The alias targets are clean, consistent, and familiar to any developer used to working with mainstream CMake-based libraries.
- The namespace prevents target name collisions with other projects.
Final Note
While there’s no strict enforcement, following these conventions will make your CMake configuration more readable, maintainable, and intuitive for other developers. Most popular CMake-powered projects adhere to these rules, so aligning with them helps your library fit seamlessly into the broader ecosystem.
内容的提问来源于stack exchange,提问作者MikeMB

