[CMake] Using find_package() portably?

Carsten Fuchs carsten.fuchs at cafu.de
Tue Nov 21 03:00:55 EST 2017


Dear group,

I'm new to CMake and read all related tutorials that I could find and 
the Mastering CMake book (I didn't get very far) and watched Daniel 
Pfeifer's talk. Unfortunately, I'm afraid that I still cannot figure out 
a couple of apparently simple things, especially in today's "best 
practices" style.

Please consider:

     find_package(JPEG REQUIRED)

At first, this didn't work for me under Ubuntu Linux, because I didn't 
have the libjpeg-dev package installed. The error message was:

CMake Error at 
/usr/share/cmake-3.9/Modules/FindPackageHandleStandardArgs.cmake:137 
(message):
   Could NOT find JPEG (missing: JPEG_LIBRARY JPEG_INCLUDE_DIR)
Call Stack (most recent call first):
   /usr/share/cmake-3.9/Modules/FindPackageHandleStandardArgs.cmake:377 
(_FPHSA_FAILURE_MESSAGE)
   /usr/share/cmake-3.9/Modules/FindJPEG.cmake:30 
(FIND_PACKAGE_HANDLE_STANDARD_ARGS)
   Libs/CMakeLists.txt:5 (find_package)

Well, I provoked this intentionally for learning and testing, but if I 
hadn't been prepared, from the error message alone I would not have 
known how to proceed. `sudo apt install libjepg-dev` fixed the problem.

Now I'm under Windows, Visual Studio Community 2017. Same error message, 
and obviously no solution as easy as `sudo apt ...`.
It's quite clear that I have to add the libjpeg source code as a 
subfolder to my project and compile it as part of the main project. But, 
starting with the above `find_package(JPEG REQUIRED)`, how is this 
properly done?

Generalizing my question:

I started with `find_package(JPEG REQUIRED)` as shown above in order to 
the find the JPEG library that comes preinstalled with the system.
How do I deal with the following scenarios?

   - libjpeg is theoretically available, but just not installed in the 
system (that is, `sudo apt install libjpeg-dev` would help)?

   - libjpeg is available in the system, but the version is too new or 
too old?

   - libjpeg is not available in the system at all (e.g. Windows)?

   - As a fallback, in the case that any of the above checks is 
negative, it seems that there is no alternative but to ship and compile 
a copy of libjpeg along with my project: How is this properly expressed 
in my project's CMakeLists.txt? Daniel mentioned something about the 
behavior of `find_package()` in his talk, but I'm afraid that I still 
cannot come up with a proper solution.

Best regards,
Carsten



More information about the CMake mailing list