[CMake] What are the actual benefits of namespaced targets?

Nils Gladitz nilsgladitz at gmail.com
Sun Mar 11 05:15:21 EDT 2018


On 10.03.2018 23:01, Alan W. Irwin wrote:
> Anyhow, your thoughts would be appreciated for reasonable best
> practice limits on how far (if any) beyond the common code case you
> would go to convert an old project to use
> ALIAS libraries and modules in the build tree that have to be
> implemented in any case for CMake code that is common to
> both the build tree and install tree. 


Hello Alan,

I agree with your assessments.

Assuming a project that is highly structured through directory scopes I 
might consider the following.

Any target is defined in exactly one directory scope.
Any command that extends the definition of a target has to use the 
original target name and should be in the same directory scope.

In all other directory scopes I'd consider consistent use of a target's 
alias.

While it is less likely for a seasoned developer to trip over the 
interpretation ambiguity in link libraries (due to familiarity and the 
fact that they probably already tripped over this and know the symptoms) 
it might still be beneficial for new / casual contributors which as 
likely consumers might also already be familiar with the aliases.

One minor additional benefit might be that you are also not allowed to 
modify the original target through an alias.
As such this would prevent you (assuming you stick to the namespaced 
names) from modifying targets from within foreign scopes which I think 
is a good thing to enforce.

Beyond that consumers might be looking at your code base for examples on 
how to use the libraries in their own code where consistency might be 
beneficial.
It might also help (slightly) if you ever decided to split off parts of 
your project into new projects (which then would become consumers of the 
exported targets).

Nils


More information about the CMake mailing list