[CMake] Using find_package() portably?

Konstantin Tokarev annulen at yandex.ru
Wed Nov 22 05:44:04 EST 2017



22.11.2017, 13:37, "Carsten Fuchs" <carsten.fuchs at cafu.de>:
> Hi all,
>
> thanks to you all for your replies!
>
> Concluding from your replies, it seems that the following approach for
> each external dependency implements a good(?) solution:
>
> find_package(JPEG) # without "REQUIRED"
>
> if (NOT JPEG_FOUND)
>      # Optionally, under Linux only:
>      # Stop with custom error message that explains how to install libjpeg.
>      # Or ask if alternatively the shipped library should be used.
>      # This complements the installation instructions in the user docs.
>      ... # TODO!
>
>      # Proceed with the shipped copy of libjpeg:
>      # Always under Windows

Note that there are package systems on Windows too, e.g. VCPKG or MSYS2.

Also there are a few packages which can be found by CMake by using registry
keys provided by their installers (not the case for libjpeg)


> and possibly under Linux as outlined above.
>      # Always in source code form, compiling here; not pre-made binaries.
>      # (This approach is my personal preference over the other options
>      # that have been suggested.)
>      add_subdirectory(../ExtLibs/libjpeg ../ExtLibs/libjpeg)
>
>      ... # see below
> endif()
>
> # How does this work in case `NOT JPEG_FOUND` above?
> target_link_libraries(main_program ${JPEG_LIBRARIES})
>
> That is, follow-up question: near the "..." above, we have got a target
> "libjpeg", but how do I get the variables JPEG_INCLUDES, JPEG_LIBRARIES
> etc. that `find_package(JPEG)` normally provides so that the last line
> with the `target_link_libraries()` works?
> I guess that defining them in `../ExtLibs/libjpeg/CMakeLists.txt` is wrong.
>
> It seems to me that the 2017 talks by Mathieu Roperts and Daniel Pfeifer
> (starting at slide 53, see especially slide 64 at
> https://www.slideshare.net/DanielPfeifer1/effective-cmake) suggest that
> this is how the modern, modular CMake is supposed to work?
>
> Best regards,
> Carsten
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake

-- 
Regards,
Konstantin


More information about the CMake mailing list