1

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?

squareskittles
  • 11,997
  • 7
  • 31
  • 48
Soo
  • 805
  • 6
  • 20

2 Answers2

2

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}")
squareskittles
  • 11,997
  • 7
  • 31
  • 48
1

This is because CMake expects a list of components. That is, a string where each item is separated by a ;. If you instead do set(SelectedQt4Packages "QtCore;QtNetwork") and change the call to configureQt4( 4.8 "${SelectedQt4Packages}") (note the double quotes), it should work as expected.

Edit: A cleaner solution would be to simply convert the argument to a list inside the macro:

# Now we can set selectedPackages to either "QtCore QtNetwork" or "QtCore;QtNetwork", both will work.
macro(configureQt4 requiredVersion selectedPackages)
    message(STATUS "selectedPackages: ${selectedPackages}")
    string(REPLACE " " ";" _selectedQtPackages ${selectedPackages})
    find_package(Qt4 ${requiredVersion} COMPONENTS ${_selectedQtPackages} REQUIRED )
endmacro()
thomas_f
  • 1,572
  • 14
  • 23