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

P F pfultz2 at yahoo.com
Sat Jul 1 09:47:55 EDT 2017


> On Jun 30, 2017, at 6:40 AM, Louis-Paul CORDIER <lp.cordier at dynamixyz.com> wrote:
> 
> 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?

This probably should be the case, but I think a lack of contributions is the problem with the current Find modules not being update on each module.

> 2. If not, is it a good practice to use add_library() when writing our FindXXX.cmake package?

When writing your own FindXXX.cmake, you should first report a bug to library XXX for not supporting cmake downstream. Also, the purpose of `find_package` is to find prebuilt binaries, its not to add new libraries to be built.

> 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?

Yes, it is good practice to make your dependencies imported targets as it allows for the dependencies to be relocatable.

Here is some more information about cmake packages:

https://cmake.org/cmake/help/v3.7/manual/cmake-packages.7.html




More information about the CMake mailing list