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

So far I’ve tried the following three different ways to call 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,

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


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.