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

Louis-Paul CORDIER lp.cordier at dynamixyz.com
Mon Jul 3 04:33:33 EDT 2017


(My answers to your previous email are below this new question.)

I have another question thus: some libraries, especially on Windows, 
provide both static and shared library. Is there any way for selecting 
which kind of library we want to get?
For instance:

LibJPEGTurbo has (turbojpeg.lib + turbojpeg.dll) and 
(turbojpeg-static.lib). So in the find_package, we can do

1. shared version
add_library(LibJPEGTurbo SHARED IMPORTED)
set_target_properties(LibJPEGTurbo PROPERTIES IMPORTED_IMPLIB 
"turbojpeg.lib")
set_target_properties(LibJPEGTurbo PROPERTIES IMPORTED_LOCATION 
"turbojpeg.dll")

2. static version
add_library(LibJPEGTurbo STATIC IMPORTED)
set_target_properties(LibJPEGTurbo PROPERTIES IMPORTED_LOCATION 
"turbojpeg-static.lib")

Maybe setting a variable before calling find_package, like 
set(FIND_SHAREDLIBS TRUE)?


Le 01/07/2017 à 15:47, P F a écrit :
>> 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.

Thanks, this should be added to the documentation of FindPackage then so 
people can start getting good habits, shouldn't be?
I guess of your answer that every find_package() should populate 
LOCATION_ properties.

>
>> 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.
Unfortunately, many people using CMake build system don't know how to 
write a proper CMakeLists.txt as there are plenty ways to workaround the 
exporting process of CMake. Also many project are not using CMake at all.
When I mean the add_library command, I am talking about the IMPORTED 
function of it, meaning that the library is already built and shouldn't 
create a new target in the Makefile/IDE.
>> 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