[CMake] find_package REQUIRED ignores OPTIONAL_COMPONENTS

Jakob van Bethlehem jsvanbethlehem at gmail.com
Tue Mar 8 10:34:11 EST 2016


Hej Alexander,

Yes, you're missing a subtle detail. You assume that the 'REQUIRED' keyword
reflects the fact that COMPONENTS are required. This is not the case. The
REQUIRED keyword reflects that the entire package Qt4 is required, see
https://cmake.org/cmake/help/v3.0/command/find_package.html where it says:
'The REQUIRED option stops processing with an error message if the package
cannot be found'

Confusing, I agree.

Sincerely,
Jakob

On Fri, Mar 4, 2016 at 9:18 AM, Alexander Stein <
alexander.stein+cmake at mailbox.org> wrote:

> Hi,
>
> I want to use some required Qt component while others are optional.
> Apparently if you specify REQUIRED in find_package OPTIONAL_COMPONENTS is
> ignored.
> Here is a minimal CMakeLists.txt:
> project(test)
> cmake_minimum_required(VERSION 3.5)
>
> find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui OPTIONAL_COMPONENTS
> Invalid)
>
> /home/alex/repo/cmake/build/bin/cmake --version
> cmake version 3.5.20160303-gf37f
>
> cmake fails with:
> CMake Error at
> /home/alex/repo/cmake/Modules/FindPackageHandleStandardArgs.cmake:148
> (message):
>   Could NOT find Qt4 (missing: QT_INVALID_INCLUDE_DIR QT_INVALID_LIBRARY)
>   (found version "4.8.7")
> Call Stack (most recent call first):
>   /home/alex/repo/cmake/Modules/FindPackageHandleStandardArgs.cmake:388
> (_FPHSA_FAILURE_MESSAGE)
>   /home/alex/repo/cmake/Modules/FindQt4.cmake:1333
> (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
>   CMakeLists.txt:4 (find_package)
>
> I expected that if required components are missing cmake bails out while
> continuing when OPTIONAL_COMPONENTS are missing. My current workaround is:
> find_package(Qt4 OPTIONAL_COMPONENTS Invalid)
> find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui)
>
> But I would rather use a single line. Am I missing something here?
>
> Best regards,
> Alexander
>
> --
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160308/ccb14ad0/attachment.html>


More information about the CMake mailing list