CMake & Qt4 Modules

Posted April 8th @ 5:04 pm by da-crystal

Don’t Reinvent the wheel!

After digging in CMake modules, and more specifically Qt once ( FindQ4.cmake , UseQt4.cmake also FindQT.cmake).  I found something really interesting in those modules!! They are smart modules ;) .  If you read my previous post CMake & Qt4 (Q3Support) and read this, you will see how stupid I was by “Reinventing the wheel” ;) .

 

Let get to the point!!

As we know ( I’m assuming you are familiar with Qt)  that Qt having many Modules e.g. ( Core, GUI ,Qt3Support,Network,SQL,XML,Test )  and lot other. And not necessarily that you need all of them in one project. It depends on the needs of your project.  Anyway, FindQt4 & UseQt4 come with very handy Macros to defined and include those modules with clean and cleaver way.

Basically you need three steps to instruct CMake about Qt.

First step tell CMake what Qt Modules you need. By setting Qt Modules’ variable to ‘true’. And the following are the available Qt Modules’ variable:

  • QT_USE_QT3SUPPORT
  • QT_USE_QTASSISTANT
  • QT_USE_QTDESIGNER
  • QT_USE_QTMOTIF
  • QT_USE_QTMAIN
  • QT_USE_QTNETWORK
  • QT_USE_QTNSPLUGIN
  • QT_USE_QTOPENGL
  • QT_USE_QTSQL
  • QT_USE_QTXML
  • QT_USE_QTSVG
  • QT_USE_QTTEST
  • QT_USE_QTUITOOLS
  • QT_USE_QTDBUS
  • QT_USE_QTSCRIPT

e.g.:

SET(QT_USE_QT3SUPPORT true)

Note that ‘Core’ and ‘GUI’ Modules libraries are loaded by default.  In case you don’t need to load them you can see the following variable as need to true:

  • QT_DONT_USE_QTCORE
  • QT_DONT_USE_QTGUI

 

Next step is to include the Qt need files. This done by only one statement:

INCLUDE(${QT_USE_FILE})

 

And the last step is the link the libraries need. This done by adding ‘${QT_LIBRARIES}’ to linking statement.

e.g.:

TARGET_LINK_LIBRARIES(CMakifyQt3S ${QT_LIBRARIES} )

 

Example:

If you recall CMakeLists.txt from”CMake & Qt4 (Q3Support) “:

# Qt Includes
    INCLUDE(${QT_USE_FILE})
    INCLUDE_DIRECTORIES(${QT_QT3SUPPORT_INCLUDE_DIR})

# Current Project Include
    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

# Generating UI’s
    FILE(GLOB Forms_UIS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UI/*.ui)
    QT4_WRAP_UI(Forms_UIS_H ${Forms_UIS})

# Sources files
    FILE(GLOB Sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

# Qt Definitions
    ADD_DEFINITIONS(${QT_DEFINITIONS})
    ADD_DEFINITIONS(-DQT3_SUPPORT)

# Output
    SET(ALLSRC ${Sources} ${Forms_UIS_H})
    ADD_EXECUTABLE(CMakifyQt3S ${ALLSRC})
    TARGET_LINK_LIBRARIES(CMakifyQt3S
                  ${QT_LIBRARIES}
                  ${QT_QT3SUPPORT_LIBRARY}
     )

So after fully utilizing CMake Qt’ Modules, the CMakeLists.txt will be as follow:

# Qt Modules
    SET(QT_USE_QT3SUPPORT true)

# Qt Includes
    INCLUDE(${QT_USE_FILE})

# Current Project Include
    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

# Generating UI’s
    FILE(GLOB Forms_UIS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UI/*.ui)
    QT4_WRAP_UI(Forms_UIS_H ${Forms_UIS})

# Sources files
    FILE(GLOB Sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

# Output
    SET(ALLSRC ${Sources} ${Forms_UIS_H})
    ADD_EXECUTABLE(CMakifyQt3S ${ALLSRC})
    TARGET_LINK_LIBRARIES(CMakifyQt3S ${QT_LIBRARIES})

No Comments Yet

You can be the first to comment!

Sorry, comments for this entry are closed at this time.