[CMake] How does :: actually works?

Johannes Zarl-Zierl johannes.zarl-zierl at jku.at
Mon Jun 15 12:20:39 EDT 2015


On Monday 15 June 2015 16:40:10 Klaim - Joël Lamotte wrote:
> So far I didn't get what imported targets helps with, so I'm a bit lost on
> the usefulness of "namespaces"
> but I am sure that I am missing something.

Imported targets improve the handling of find_package stuff. In the "old days", 
the common pattern for using another library as dependency was as follows:

find_package(XXX)
include_directories( ${XXX_INCLUDE_DIR} )
add_definitions( ${XXX_DEFINITIONS} )
target_link_library(YourTarget ${XXX_LIBRARIES} )

Sometimes you don't need the add_definitions part, and sometimes the variable 
names are a little bit different. Most of the time you inadvertedly add 
definitions and include directories for other targets, too.


With imported targets, you can combine everything that's needed to use a 
library into an imported target, making the above pattern simpler and less 
error-prone:

find_package(XXX)
target_link_library(YourTarget XXX)

With more complex libraries, you often have different components, and that's 
when namespaces come in handy:

find_package(XXX COMPONENTS Base Advanced)
target_link_library(YourTarget XXX::Base)
Find_package(YYY COMPONENTS Base)
target_link_library(YourOtherTarget XXX::Advanced YYY::Base)

Does this answer your question?

Cheers,
  Johannes





More information about the CMake mailing list