[CMake] find_package(), FindXXX.cmake and IMPORTED add_library()

Louis-Paul CORDIER lp.cordier at dynamixyz.com
Fri Jun 30 07:40:24 EDT 2017


Hi,

I'm particularly familiar with find_package() command, add_library(... 
IMPORTED) and find_library(). However, I found there are many 
differences on find_package() usage depending of the library being imported.

For instance, using find_package() on Qt5 will retrieve a bunch of 
*Config.cmake files in the Qt installation tree, and add each components 
as a library using add_library(Qt5::COMPONENT SHARED IMPORTED).
One nice feature with that is the possibility to retrieve the LOCATION 
property on each component to get the DLL file.

That said when using find_package(sharedLibFOO) that will make use of 
hand-written FindsharedLibFOO.cmake, some variables like 
FindsharedLib_FOUND, FindsharedLib_LIBRARIES, FindsharedLib_VERSION 
(etc.) are set by the script, but there is almost never add_library() 
command used inside this CMake find script.

1. Should it be mandatory to use add_library() in FindXXX.cmake scripts, 
so the user just needs to target_link_libraries() and all compile 
definitions, includes dir, and lib dir are automatically imported?
2. If not, is it a good practice to use add_library() when writing our 
FindXXX.cmake package?
3. When making an application, is it a good practice to create an 
imported target for each library, instead of appending manually to the 
current project using target_link_libraries(), 
target_compile_definitions(), target_include_directories() and so on?

Thank you for your lights,
BR

Louis-Paul CORDIER


More information about the CMake mailing list