Qt3 with cmake
From qtnode
cmake for Qt/KDE 3
Note: we will require cmake 2.4 here. However some/most of the code below may work with previous versions, too. Here we have a look at a simple cmake based project for Qt3/KDE3. I am going to use the KDE3 macros, because I come from a KDE background and I find them much simpler to use, than the plain Qt variants, but I think you don't actually need KDE for them.
Suppose we have a simple project:
- qtproject
- src
- main.cpp
- top.cpp
- top.h
- src
To turn it into a cmake project, we add two CMakeLists.txt files, one per directory. The qtproject/CMakeLists.txt file will look like this:
project(qtproject) # the name of your project
cmake_minimum_required(VERSION 2.4.0)
find_package(Qt3 REQUIRED) # find and setup Qt3 for this project
find_package(KDE3 REQUIRED) # find and setup KDE3 for this project
add_definitions(${QT_DEFINITIONS} ${KDE3_DEFINITIONS})
# tell cmake where to search for libraries:
link_directories(${KDE3_LIB_DIR})
# tell cmake where to search for Qt/KDE headers:
include_directories(${KDE3_INCLUDE_DIR} ${QT_INCLUDE_DIR})
# tell cmake to process CMakeLists.txt in that subdirectory
add_subdirectory(src)
A closer look at some macros used here:
- find_package(Qt3 REQUIRED): this is some kind of configure test. It searches for Qt3 and defines a couple of variables that we will use later on. This will be required for your Qt3 based project.
- find_package(KDE3 REQUIRED): this does the same for KDE3. Skip this if your project is Qt only.
- link_directories() this adds directories to the path where cmake searches for libraries, nothing else. Pretty much a -L<directory> when it comes to g++.
- include_directories() this does not "include" a directory to cmake, but rather tells the compiler where to search for include files. I.e. -I<directory> when it comes to g++.
The actually interesting things happen in qtproject/src/CMakeLists.txt:
# the variable "qtproject_SRCS" contains all .cpp files of this project
set(qtproject_SRCS
main.cpp
top.cpp
)
# tell cmake to create .moc files for all files in the variable qtproject_SRCS that require such a file.
# note: this assumes that you use #include "header.moc" in your files
# also note that you don't actually require kde to use this command
kde3_automoc(${qtproject_SRCS})
# create an executable file named "qtproject" from the source files in the variable "qtproject_SRCS".
add_executable(qtproject ${qtproject_SRCS})
# link the "qtproject" target (i.e. the executable file added above) agains the Qt and the kdecore libraries.
target_link_libraries(qtproject ${QT_AND_KDECORE_LIBRARIES})
- set(<variable> <value>): this is pretty much a "<variable> = <value>" statement. The first argument is the variable name, the rest is a list (space or newline separated) of values for it. I just emphasize it, because it looked cryptic to me a long time ago :-)
- kde3_automoc(): add #include "header.moc" next to your #include "header.h" and then use this macro. It will do all the moc work for you.
- target_link_libraries(qtproject ${QT_AND_KDECORE_LIBRARIES}): link qtprojects against some libraries. You can add more to that list, adding library names (just the name, neither a prefix such as "lib", nor a suffix, such as ".so") to it (space separated). Instead of $ {QT_AND_KDECORE_LIBRARIES} you could use (for example):
- ${QT_LIBRARIES} - only the Qt libraries, no KDE libraries
- ${QT_AND_KDECORE_LIBRARIES} kdeui - create a KDE GUI program
Sample Projects
Qt based projects using cmake: