[CMake] Using find_package() portably?

Carsten Fuchs carsten.fuchs at cafu.de
Wed Nov 22 05:32:22 EST 2017


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


More information about the CMake mailing list