find_package COMPONENTS with variable cmake list

Hi everybody,

I would like to define a basic find component system for all of my projects which has some default componet values set but may be edited by the user in cmake gui if the need arises.

So far I thought I would define a cmake CACHE STRING variable with the default components e.g.
set(QT_COMPONENTS “Core Gui Widgets” CACHE STRING “Project default QT components”)

and then call find_package using the component string
find_package(Qt5 REQUIRED COMPONENTS “${QT_COMPONENTS}”)

So far I’ve tried the following three different ways to call COMPONENTS:
“${QT_COMPONENTS}”
${QT_COMPONENTS}
QT_COMPONENTS

But, no call seems to be correct. I can’t seem to get the find_package function evaluate the QT_COMPONENTS property.

Printing “${QT_COMPONENTS}” results in → Core Gui Widgets, which seems to be fine.
In the gui I get the new variable “Qt5Core Gui Widgets Network_DIR-NOTFOUND” so my string does not seem to be handled as separate values with witespaces.

Is there a way this will work?

Best regards,
Matthias

set(QT_COMPONENTS “Core;Gui;Widgets” CACHE STRING “Project default QT components”)

or

set(QT_COMPONENTS Core Gui Widgets CACHE STRING “Project default QT components”)

1 Like

If you need the CACHE variable to be space-separated, use string() to replace spaces with semicolons.