[CMake] Using find_package() portably?

Konstantin Tokarev annulen at yandex.ru
Tue Nov 21 03:45:43 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.

find_package() allows you to use external libraries (or other kinds of files for that matter)
which are not shipped with your source code. It knows nothing about how to install the
missing dependencies, or if this is possible at all (usually possible, because you can
compile dependencies from sources and maybe even share resulting binaries with other
developers). Instead, you can

1) add instruction how to install dependencies
2) use hand-made script or more complex solution to automate dependency installation, or 
3) avoid using find_package() st all

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