How to create ALIAS targets for all of my libs in one command?

I’m in the process of porting my project from GNU Make to CMake, and am now creating the exported CMake file. I want to add all of my exported libraries into a namespace, since that appears to be good practice. However, my understanding is that for “best results” I should also create ALIAS targets for the exported libraries.

Does that mean I have to manually add a line like add_library(MyProj::foo ALIAS foo) for every exported library? Or is there a way to have ALIAS targets created automatically for all of my libraries, with a few lines of code? Adding all of these ALIAS targets manually looks error-prone and wordy to me.

You can use a loop:

foreach(lib IN ITEMS foo bar baz qux)
  add_library(MyProj::${lib} ALIAS ${lib})
1 Like