Search code examples
cmakeqt4cmake-language

Problem using Qt4 with find_package of CMake, inside a macro


I have defined the following macro in CMake (version 3.10):

macro(configureQt4 requiredVersion selectedPackages)
    message(STATUS "selectedPackages: ${selectedPackages}")
    find_package(Qt4 ${requiredVersion} COMPONENTS ${selectedPackages} REQUIRED ) 
endmacro()

Now, when I tried to call the macro in the following way, I get an error:

set(SelectedQt4Packages "QtCore QtNetwork")
configureQt4( 4.8 ${SelectedQt4Packages})

The error reported is:

CMake Error at /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find Qt4 (missing: QT_QTCORE QTNETWORK_INCLUDE_DIR QT_QTCORE
  QTNETWORK_LIBRARY) (found suitable version "4.8.7", minimum required is
  "4.8")

If I call find_package() in the following way inside the macro, it works!

find_package(Qt4 ${requiredVersion} COMPONENTS QtCore QtNetwork REQUIRED )

But I need to use it by setting a variable as discussed earlier. How can I resolve this issue?


Solution

  • If you want to set a list variable in CMake, you can achieve this by excluding the quotes:

    set(SelectedQt4Packages QtCore QtNetwork)
    

    Using quotes like this "QtCore QtNetwork" simply creates a string with a space between the two component names, which is likely not what you intend.

    Now, you can pass the SelectedQt4Packages list variable to your macro, but be sure to surround it with quotes (as suggested in this answer):

    set(SelectedQt4Packages QtCore QtNetwork)
    configureQt4( 4.8 "${SelectedQt4Packages}")