Development/Tutorials/CMake/KDE4: Difference between revisions

From KDE TechBase
m (Pippin moved page Development/Tutorials/CMake to Development/Tutorials/CMake/KDE4: Does not apply to post-kdelibs4 projects, and I want a landing page for multiple tutorials.)
 
(28 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/CMake}}


== Introduction ==
== Introduction ==
CMake reads script files and produces input files for the native buildsystem of the platform where it runs on. It can create GNU Makefiles, KDevelop project files, XCode project files, and Visual Studio project files.
[http://www.cmake.org CMake] reads script files and produces input files for the native buildsystem of the platform where it runs on. It can create GNU Makefiles, KDevelop project files, XCode project files, and Visual Studio project files.


CMake is free software and released under a BSD-style license.  It is developed by [http://www.kitware.com Kitware Inc.]
[http://www.cmake.org CMake] is free software and released under a BSD license.  It is developed by [http://www.kitware.com Kitware Inc.]


Here are some good places to learn about CMake in general:
You can find more CMake related information on the [[Development/CMake | main CMake page]] here on TechBase.
* [http://www.cmake.org/HTML/Documentation.html CMake Manual]
* [http://www.cmake.org/Wiki/CMake CMake Wiki]
* [http://www.cmake.org/Wiki/CMake_HowToDoPlatformChecks CMake System Checks ]
* [http://www.cmake.org/Wiki/CMake_HowToFindInstalledSoftware Finding Optional Packages ]
* [http://www.cmake.org/Wiki/HowToBuildKDE4Software KDE 4 Tutorial at the CMake Wiki ]
* [http://www.cmake.org/Wiki/CMake_FAQ CMake FAQ]
* [http://www.elpauer.org/stuff/learning_cmake.pdf Slides for the "Learning CMake" seminar]
* [http://mail.kde.org/pipermail/kde-buildsystem/ The kde-buildsystem Mailing List Archive]
 
Also, consider joining the [http://www.cmake.org/mailman/listinfo/cmake CMake mailing list].


== Why use CMake ? ==
== Why use CMake ? ==
Line 28: Line 17:
=== Get and install CMake ===
=== Get and install CMake ===
==== Linux, BSD, and other Unix Systems ====
==== Linux, BSD, and other Unix Systems ====
Retrieve the latest stable version of CMake from [http://www.cmake.org/HTML/Download.html].
Retrieve the latest stable version of CMake from [http://www.cmake.org/HTML/Download.html here].


Once downloaded, unpack and compile it:
Once downloaded, unpack and compile it:
<code>
<syntaxhighlight lang="bash">
$ mkdir cmake-build
$ mkdir cmake-build
$ cd cmake-build
$ cd cmake-build
Line 37: Line 26:
$ make
$ make
$ make install
$ make install
</code>
</syntaxhighlight>


By default, this will install CMake in /usr/local, so make sure to have /usr/local/bin in your execute path.  To change the installation prefix (e.g. to /usr in debian), add the '--prefix=PATH' option to the bootstrap command.
By default, this will install CMake in /usr/local, so make sure to have /usr/local/bin in your execute path.  To change the installation prefix (e.g. to /usr in debian), add the '--prefix=PATH' option to the bootstrap command.
Line 44: Line 33:


==== Windows ====
==== Windows ====
Retrieve the latest stable version of CMake from [http://www.cmake.org/HTML/Download.html].
Retrieve the latest stable version of CMake from [http://www.cmake.org/HTML/Download.html here].


Once downloaded, run the cmake installer.  
Once downloaded, run the cmake installer.  


By default, this will install CMake in C:\Program Files\CMake 2.4, so make sure to have <installpath>\bin in your execute path.   
By default, this will install CMake in C:\Program Files\CMake 2.8, so make sure to have <installpath>\bin in your execute path.   


Please follow the instructions at [http://cmake.org/HTML/Download.html#cvs here] if you would like to use the current development version.
Please follow the instructions at [http://cmake.org/HTML/Download.html#cvs here] if you would like to use the current development version.
Line 58: Line 47:


So, let's say you have kdelibs/ in ~/src/kdelibs/, then do the following:
So, let's say you have kdelibs/ in ~/src/kdelibs/, then do the following:
<code>
<syntaxhighlight lang="bash">
$ ls
$ ls
kdelibs/
kdelibs/
Line 64: Line 53:
$ cd kdelibs-build
$ cd kdelibs-build
$ cmake ../kdelibs
$ cmake ../kdelibs
</code>
</syntaxhighlight>
This will generate the Makefiles for building kdelibs/ in kdelibs-build/.
This will generate the Makefiles for building kdelibs/ in kdelibs-build/.


Line 71: Line 60:


So, let's say you have kdelibs\ in c:\daten\kde4, then do the following:
So, let's say you have kdelibs\ in c:\daten\kde4, then do the following:
<code>
<syntaxhighlight lang="dos">
c:\daten\kde4> cd kdelibs\win
c:\daten\kde4> cd kdelibs\win
c:\daten\kde4> cmake
c:\daten\kde4> cmake
Line 80: Line 69:
c:\daten\kde4> cd kdelibs-build
c:\daten\kde4> cd kdelibs-build
c:\daten\kde4\kdelibs-build> cmake ..\kdelibs
c:\daten\kde4\kdelibs-build> cmake ..\kdelibs
</code>
</syntaxhighlight>


This will generate the Makefiles for building kdelibs\ in kdelibs-build\. See [[Projects/KDE_on_Windows|KDE on Windows]] for more informations about compiling KDE on Windows.
This will generate the Makefiles for building kdelibs\ in kdelibs-build\. See [[Projects/KDE_on_Windows|KDE on Windows]] for more informations about compiling KDE on Windows.


==== KDevelop Project Files ====
==== KDevelop 3 Project Files ====
If you prefer project files for KDevelop (which will basically be Makefiles accompanied by some extra files for KDevelop), run it like this:
If you prefer project files for KDevelop 3 (which will basically be Makefiles accompanied by some extra files for KDevelop), run it like this:
<code>
<syntaxhighlight lang="bash">
$ cmake ../kdelibs -GKDevelop3
$ cmake ../kdelibs -GKDevelop3
</code>
</syntaxhighlight>
Use 'cmake -h' to find out which generators CMake supports and the other options.
Use 'cmake -h' to find out which generators CMake supports and the other options.


Line 108: Line 97:
Some cmake command line variables you may want to set:
Some cmake command line variables you may want to set:
* '''CMAKE_INSTALL_PREFIX''': cmake ../kdelibs -DCMAKE_INSTALL_PREFIX=/opt/kde4 is the equivalent to ./configure --prefix=/opt/kde4
* '''CMAKE_INSTALL_PREFIX''': cmake ../kdelibs -DCMAKE_INSTALL_PREFIX=/opt/kde4 is the equivalent to ./configure --prefix=/opt/kde4
* '''CMAKE_BUILD_TYPE''': decide which type of build you want. You can chose between "debugfull", "debug", "profile", "relwithdebinfo" and "release". The default is "relwithdebinfo". See FindKDE4Internal.cmake for details and the actual flags used.
* '''[[Development/CMake/Addons_for_KDE#Buildtypes|CMAKE_BUILD_TYPE]]''': decide which type of build you want. You can choose between "DebugFull", "Debug", "Profile", "RelWithDebInfo" and "Release". The default is "RelWithDebInfo". Please refer to page [[Development/CMake/Addons_for_KDE#Buildtypes|CMake Build Types]] for a more detailed explanation.
** "release": optimized for speed, no debug symbols or debug messages
** "relwithdebinfo": optimized for speed, debug symbols for backtraces
** "debug": optimized, but debuggable
** "debugfull": no optimisations, full debug support
** "profile": adds coverage flags to debugfull
* '''KDE4_BUILD_TESTS=ON''': creates Makefiles with build test programs and also provides 'test' targets
* '''KDE4_BUILD_TESTS=ON''': creates Makefiles with build test programs and also provides 'test' targets
* '''KDE4_TEST_OUTPUT=xml''': Unit tests using the QTestLib framework will create xml formatted logfiles.
* '''KDE4_TEST_OUTPUT=xml''': Unit tests using the QTestLib framework will create xml formatted logfiles.
* '''KDE4_DISABLE_MULTIMEDIA=ON''': Build KDE without any multimedia (audio and video) support.
* '''KDE4_DISABLE_MULTIMEDIA=ON''': Build KDE without any multimedia (audio and video) support.
* '''RPATH_STYLE''': This is not available anymore. By default everything will be built with RPATH set correctly. If you don't want RPATH at all, set CMAKE_SKIP_RPATH to TRUE.
* '''BUILD_foo=OFF''': disables the build for the project in subdirectory 'foo'.
* '''BUILD_foo=OFF''': disables the build for the project in subdirectory 'foo'.
* '''WITH_foo''': there are several options, e.g. WITH_CUPS or WITH_Jasper. If you disable them, cmake will not even try to find this package. If it is enabled, cmake will try to find it. If it fails with this, you can still adjust it manually as described above.
* '''WITH_foo''': there are several options, e.g. WITH_CUPS or WITH_Jasper. If you disable them, cmake will not even try to find this package. If it is enabled, cmake will try to find it. If it fails with this, you can still adjust it manually as described above.


=== Environment Variables ===
=== Environment Variables ===
If you have headers and libraries installed in non-standard locations that cmake cannot find (e.g., fink on Mac OSX installs to /sw), then set the following as environment variables.  This can be also very
If you have headers and libraries installed in non-standard locations that cmake cannot find (e.g., fink on Mac OSX installs to /sw), then populate the environment variable CMAKE_PREFIX_PATH with a list of paths to these locationsWhen searching for headers, libraries and binaries, CMake will search all the paths in CMAKE_PREFIX_PATH as well as their standard subdirectories ("lib" for libraries, "include" for headers and "bin" for binaries). This can be also very useful e.g. if you install kdesupport to ~/install/kdesupport.
useful e.g. if you install kdesupport to ~/install/kdesupport .
Despite the similar naming convention, these will not work as arguments on the cmake command line:
* '''CMAKE_INCLUDE_PATH''', eg. export CMAKE_INCLUDE_PATH=/sw/include
* '''CMAKE_LIBRARY_PATH''', eg. export CMAKE_LIBRARY_PATH=/sw/lib
* '''CMAKE_PROGRAM_PATH''', eg. export CMAKE_LIBRARY_PATH=/sw/bin
 
With CMake 2.6.0 and above the same effect can be achieved by setting just one
variable:
* '''CMAKE_PREFIX_PATH''', eg. export CMAKE_PREFIX_PATH=/sw
* '''CMAKE_PREFIX_PATH''', eg. export CMAKE_PREFIX_PATH=/sw


 
If you need to further tweak the search behavior, you can additionally set the
following environment variables in order to add library, include or binary
directories to your search path:
* '''CMAKE_INCLUDE_PATH''', eg. export CMAKE_PREFIX_PATH=/sw/include
* '''CMAKE_LIBRARY_PATH''', eg. export CMAKE_LIBRARY_PATH=/sw/lib
* '''CMAKE_PROGRAM_PATH''', eg. export CMAKE_PROGRAM_PATH=/sw/bin


For more information on variables, see [http://www.cmake.org/Wiki/CMake_Useful_Variables this cmake.org wiki page]
For more information on variables, see [http://www.cmake.org/Wiki/CMake_Useful_Variables this cmake.org wiki page]
Line 141: Line 121:


If you got a failure that says something like
If you got a failure that says something like
<code>
<pre>
CMake Error: This project requires some variables to be set,
CMake Error: This project requires some variables to be set,
and cmake can not find them.
and cmake can not find them.
Please set the following variables:
Please set the following variables:
X11_XTest_LIB (ADVANCED)
X11_XTest_LIB (ADVANCED)
</code>
</pre>
then you may have a missing library (or other dependency). To find
then you may have a missing library (or other dependency). To find
out which library, search in the cmake/modules directory for
out which library, search in the cmake/modules directory for
the variable that cmake can't find. In the example above, it is
the variable that cmake can't find. In the example above, it is
<code>
<syntaxhighlight lang="cmake">
FIND_LIBRARY(X11_XTest_LIB Xtst ${X11_LIB_SEARCH_PATH})
find_library(X11_XTest_LIB Xtst ${X11_LIB_SEARCH_PATH})
</code>
</syntaxhighlight>
So the missing library is Xtst. You then need to find it (perhaps installing a libXtst-devel library) and re-run cmake.
So the missing library is Xtst. You then need to find it (perhaps installing a libXtst-devel library) and re-run cmake.


Line 158: Line 138:


Here's the most simple CMakeLists.txt:
Here's the most simple CMakeLists.txt:
<code>
<syntaxhighlight lang="cmake">
add_executable(hello main.cpp)
add_executable(hello main.cpp)
</code>
</syntaxhighlight>
This will create an executable named "hello" (or "hello.exe" under Windows) from the source file main.cpp. You can mix C and C++ files as you want.  You can have multiple executables and libraries in one CMakeLists.txt.  The same source file can be used in multiple targets, it will be compiled for each target independently from the other targets.  Probably the most important part of the cmake language are the variables:
This will create an executable named "hello" (or "hello.exe" under Windows) from the source file main.cpp. You can mix C and C++ files as you want.  You can have multiple executables and libraries in one CMakeLists.txt.  The same source file can be used in multiple targets, it will be compiled for each target independently from the other targets.  Probably the most important part of the cmake language are the variables:
<code>
<syntaxhighlight lang="cmake">
SET( MY_SOURCES main.cpp widget.cpp)
set( MY_SOURCES main.cpp widget.cpp)
MESSAGE(STATUS "my sources: ${MY_SOURCES}")
message(STATUS "my sources: ${MY_SOURCES}")
</code>
</syntaxhighlight>
So, use the SET() command to set the value of a variable. If you list more than one string, the variable will be a list.  A list is a list of strings separated by semicolons. If you set it to only one item, it will have just that value. To get the value of a variable, use ${VAR}.
So, use the SET() command to set the value of a variable. If you list more than one string, the variable will be a list.  A list is a list of strings separated by semicolons. If you set it to only one item, it will have just that value. To get the value of a variable, use ${VAR}.
You can iterate over a list using FOREACH():
You can iterate over a list using FOREACH():
<code>
<syntaxhighlight lang="cmake">
FOREACH(next_ITEM ${MY_SOURCES})
foreach(next_ITEM ${MY_SOURCES})
   MESSAGE(STATUS "next item: ${next_ITEM}")
   message(STATUS "next item: ${next_ITEM}")
ENDFOREACH(next_ITEM ${MY_SOURCES})
endforeach(next_ITEM ${MY_SOURCES})
</code>
</syntaxhighlight>
The commands in CMake are case-insensitive.  Names of variables and names of parameter are case-sensitive.
The commands in CMake are case-insensitive.  Names of variables and names of parameter are case-sensitive.


You can also test for various things:
You can also test for various things:
<code>
<syntaxhighlight lang="cmake">
IF (UNIX)
if (UNIX)
   MESSAGE(STATUS "This is UNIX (including OS X and CygWin)")
   message(STATUS "This is UNIX (including OS X and CygWin)")
ENDIF (UNIX)
endif (UNIX)


IF (MSVC)
if (MSVC)
   SET(MY_SRCS ${MY_SRCS} winextra.cpp)
   set(MY_SRCS ${MY_SRCS} winextra.cpp)
ENDIF (MSVC)
endif (MSVC)
</code>
</syntaxhighlight>
In this second example  you can see also how to append items to a list.
In this second example  you can see also how to append items to a list.


Line 192: Line 172:


Here's a basic CMakeList file that builds a small KDE 4 project:
Here's a basic CMakeList file that builds a small KDE 4 project:
<code>
<syntaxhighlight lang="cmake">
PROJECT(kde4project)
project( kde4project )
FIND_PACKAGE(KDE4 REQUIRED)
find_package( KDE4 REQUIRED )
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} )
include(KDE4Defaults)
 
include_directories( ${KDE4_INCLUDES} )
 
set( KDE4ProjectSources kde4mainapp.cpp someclass.cpp someotherclass.cpp )
 
kde4_add_executable( kde4project ${KDE4ProjectSources} )
 
target_link_libraries( kde4project ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )


SET(KDE4ProjectSources kde4mainapp.cpp someclass.cpp someotherclass.cpp)
install( TARGETS kde4project  ${INSTALL_TARGETS_DEFAULT_ARGS} )
</syntaxhighlight>


KDE4_ADD_EXECUTABLE(kde4project ${KDE4ProjectSources} )
''target_link_libraries'' contains the development libraries that are linked to your program. E.g. if you want to link to libtidy-devel, your library file name may be called /usr/local/lib/libtidy.a. You would then add -ltidy to a gcc call. Here, you add ''tidy'' to your target_link_libraries. If possible, use pre-defined variables or macros like ${KDE4_KDEUI_LIBS}.


TARGET_LINK_LIBRARIES(kde4project ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )
''install (TARGETS'' is where the target will finally be installed. If you do not have this line, make install will not be available.
</code>


Variables, macros and other useful information specific to KDE can be found at the [[Development/KDE and CMake Together]] page.
Variables, macros and other useful information specific to KDE can be found at the [[Development/CMake/Addons for KDE|CMake addons for KDE]] page.


== Extending CMake ==
== Extending CMake ==
Line 213: Line 201:
== Converting autotools-based KDE software to CMake ==
== Converting autotools-based KDE software to CMake ==
In kdesdk/cmake/ you can find a script am2cmake . This is a ruby script, so you need to have ruby installed. Run am2cmake in the toplevel directory of your sources:
In kdesdk/cmake/ you can find a script am2cmake . This is a ruby script, so you need to have ruby installed. Run am2cmake in the toplevel directory of your sources:
<code>
<syntaxhighlight lang="bash">
$ cd src/mykooltool/
$ cd src/mykooltool/
$ am2cmake --kde4
$ am2cmake --kde4
</code>
</syntaxhighlight>
Don't forget the switch "--kde4", otherwise it won't generate files suitable for KDE 4 software. The converted files 'may' work as they are, but complicated projects will require some additional editing.
Don't forget the switch <tt>--kde4</tt>, otherwise it won't generate files suitable for KDE 4 software. The converted files 'may' work as they are, but complicated projects will require some additional editing.


You may have to:
You may have to:
Line 227: Line 215:
* a file AdditionalInfo.txt will be created.  There you will see all *.in and *.in.in files of your project. The stuff done in these files will have to be converted manually to cmake.
* a file AdditionalInfo.txt will be created.  There you will see all *.in and *.in.in files of your project. The stuff done in these files will have to be converted manually to cmake.


== Frequently Asked Questions ==
=== How can I teach my favorite editor about CMake syntax and indentation ? ===
Read the CMake Wiki section [http://www.cmake.org/Wiki/CMake_Editors_Support CMake Editors Support].  It describes how to setup Emacs (XEmacs works too), VIM, Kate, KWrite, and KDevelop.
=== I need to generate some files during the build. How do I do this ? ===
Use ADD_CUSTOM_COMMAND(). It's explained here in the CMake wiki: [http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_generate_a_source_file_during_the_build.3F  How can I generate a source file during the build]
=== I need to build an executable which is used later on during the build to generate files. How do I do this ? ===
Let's say the executable is called genembed. Then use KDE4_ADD_EXECUTABLE(foo RUN_UNINSTALLED ${fooSources})to create the executable. The RUN_UNINSTALLED option is important, because the executable has to run from the build dir and has to link to the libraries in the builddir. To achieve this, the executable is compiled with RPATH set accordingly and a wrapper shell script, named just like the executable but with the suffix ".sh" is created. This shell scripts sets up LD_LIBRARY_PATH and the calls the actual executable.
Use this wrapper shell script in the ADD_CUSTOM_COMMAND() as described above.
You can find out the name and exact location by querying the property WRAPPER_SCRIPT. Here's a full example taken from kdelibs/kstyles/keramik/ :
<code>
# build the executable
KDE4_ADD_EXECUTABLE(genembed RUN_UNINSTALLED ${genembed_SRCS})
# get the name of the generated wrapper script (which sets up LD_LIBRARY_PATH)
GET_TARGET_PROPERTY(GENEMBED_EXECUTABLE genembed WRAPPER_SCRIPT)
# and the custom command
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/keramikrc.h
  COMMAND ${GENEMBED_EXECUTABLE} --file ${CMAKE_CURRENT_BINARY_DIR}/keramikPics.txt > \
  ${CMAKE_CURRENT_BINARY_DIR}/pixmaps.keramik
  DEPENDS genembed ${keramikPics}
)
</code>
As you can see genembed is also listed as a dependency, this means cmake knows that it has to build the executable genembed before executing this rule.
=== I don't want to set the -DCMAKE_INSTALL_PREFIX command line option.  Does cmake support the KDEDIR environment variable? ===
No.  $KDEDIR is deprecated in KDE 4.
=== Why do I get compile errors like /usr/lib/qt4/include/QtCore/qstring.h:536: undefined reference to `QString::fromLatin1_helper(char const*, int)'? ===
A: If you have an old Qt4 version in your qt/lib directory you must delete the old (4.0.1) files.
=== How do I tell cmake to create noisy makefiles?  I want to see the exact commands that are run during the make process. ===
Pass the VERBOSE variable to make, i.e.
<code>
% make VERBOSE=1
</code>
or
<code>
% VERBOSE=1 make
</code>
For more details see the CMake wiki: [http://www.cmake.org/Wiki/CMake_FAQ#Is_there_an_option_to_produce_more_.27verbose.27_compiling.3F Is there an option to produce more 'verbose' compiling?]
=== There is no 'make distclean' target in the generated Makefiles.  How do I clean up everything, including the cache files? ===
Simply remove the build directory, or just the contents of the build directory.
=== I've got ''fatal error LNK1104: cannot open file 'optimized.lib' '' under Windows/msvc (release with debug info build)  ===
'''QT_*_LIBRARIES must not be used''' in your target_link_libraries() line of CMakeLists.txt. Use QT_*_LIBRARY instead. Otherwise the variable will look like this (example for QT_QTNETWORK_LIBRARY):
<code>
optimized;C:/kde4/lib/QtNetwork4.lib;debug;C:/kde4/lib/QtNetworkd4.lib
^^^^^^^^^
</code>
The first <tt>optimized</tt> part is translated by cmake to optimized.lib so we have got the ''cannot open file 'optimized.lib' error.''


[[Category:Programming]]
[[Category:Programming]]
[[Category:Tutorial]]
[[Category:Tutorial]]
[[Category:FAQs]]
[[Category:FAQs]]

Latest revision as of 17:48, 27 July 2015

Introduction

CMake reads script files and produces input files for the native buildsystem of the platform where it runs on. It can create GNU Makefiles, KDevelop project files, XCode project files, and Visual Studio project files.

CMake is free software and released under a BSD license. It is developed by Kitware Inc.

You can find more CMake related information on the main CMake page here on TechBase.

Why use CMake ?

CMake is the official tool of KDE 4 release, decided in March 2006, primarily due to technical merits as compared to the older KDE tools automake and unsermake:

  • CMake is developed independently of KDE, it can be used by other projects as well
  • Compilation time is much faster, mainly due to not using libtool anymore
  • The build files are easier to write

How to compile KDE using CMake

Get and install CMake

Linux, BSD, and other Unix Systems

Retrieve the latest stable version of CMake from here.

Once downloaded, unpack and compile it:

$ mkdir cmake-build
$ cd cmake-build
$ ../bootstrap
$ make
$ make install

By default, this will install CMake in /usr/local, so make sure to have /usr/local/bin in your execute path. To change the installation prefix (e.g. to /usr in debian), add the '--prefix=PATH' option to the bootstrap command.

Please follow the instructions located here if you would like to use the current development version.

Windows

Retrieve the latest stable version of CMake from here.

Once downloaded, run the cmake installer.

By default, this will install CMake in C:\Program Files\CMake 2.8, so make sure to have <installpath>\bin in your execute path.

Please follow the instructions at here if you would like to use the current development version.

Run CMake

Linux, BSD, and other Unix Systems

You have to run CMake so that it generates the build files for your system. Both in-source and out-of-source builds are supported by CMake, but currently in-source builds are prevented by the KDE implementation.

So, let's say you have kdelibs/ in ~/src/kdelibs/, then do the following:

$ ls
kdelibs/
$ mkdir kdelibs-build
$ cd kdelibs-build
$ cmake ../kdelibs

This will generate the Makefiles for building kdelibs/ in kdelibs-build/.

Windows

You have to run CMake so that it generates the build files for your system. Both in-source and out-of-source builds are supported by CMake, but currently in-source builds are prevented by the KDE implementation.

So, let's say you have kdelibs\ in c:\daten\kde4, then do the following:

c:\daten\kde4> cd kdelibs\win
c:\daten\kde4> cmake
c:\daten\kde4> make 
c:\daten\kde4> make install
c:\daten\kde4> cd ..
c:\daten\kde4> mkdir kdelibs-build
c:\daten\kde4> cd kdelibs-build
c:\daten\kde4\kdelibs-build> cmake ..\kdelibs

This will generate the Makefiles for building kdelibs\ in kdelibs-build\. See KDE on Windows for more informations about compiling KDE on Windows.

KDevelop 3 Project Files

If you prefer project files for KDevelop 3 (which will basically be Makefiles accompanied by some extra files for KDevelop), run it like this:

$ cmake ../kdelibs -GKDevelop3

Use 'cmake -h' to find out which generators CMake supports and the other options.

CMake and Qt4

To locate Qt 4, CMake searches for qmake in your execute path. CMake does not use the QTDIR environment variable. So make sure that the first qmake found in the execution path is the one you like to use.

More Details

When CMake has finished, it will have created a file called "CMakeCache.txt". This file contains all the settings CMake has detected on your system. If you want to run CMake with another generator or you want CMake to detect everything again, delete this file.

If CMake didn't find something, but you know it is somewere on your box, you can tell CMake manually where to find it. CMake uses variables to store this information. These variables are cached in the already mentioned file CMakeCache.txt. You have three options to adjust these variables manually:

  • tell CMake the correct value via the command line: cmake ../kdelibs -DNAME_OF_THE_VARIABLE=value
  • use ccmake, which provides a curses based GUI to adjust the CMake variables (run: ccmake ../kdelibs)
  • edit the file CMakeCache.txt directly (not recommended)

You should run "ccmake ../kdelibs" at least once so that you get an impression which variables CMake uses. Press "T" to see also the "advanced" variables. So, if CMake didn't find something, start ccmake and adjust it manually.

Command Line Variables

Some cmake command line variables you may want to set:

  • CMAKE_INSTALL_PREFIX: cmake ../kdelibs -DCMAKE_INSTALL_PREFIX=/opt/kde4 is the equivalent to ./configure --prefix=/opt/kde4
  • CMAKE_BUILD_TYPE: decide which type of build you want. You can choose between "DebugFull", "Debug", "Profile", "RelWithDebInfo" and "Release". The default is "RelWithDebInfo". Please refer to page CMake Build Types for a more detailed explanation.
  • KDE4_BUILD_TESTS=ON: creates Makefiles with build test programs and also provides 'test' targets
  • KDE4_TEST_OUTPUT=xml: Unit tests using the QTestLib framework will create xml formatted logfiles.
  • KDE4_DISABLE_MULTIMEDIA=ON: Build KDE without any multimedia (audio and video) support.
  • BUILD_foo=OFF: disables the build for the project in subdirectory 'foo'.
  • WITH_foo: there are several options, e.g. WITH_CUPS or WITH_Jasper. If you disable them, cmake will not even try to find this package. If it is enabled, cmake will try to find it. If it fails with this, you can still adjust it manually as described above.

Environment Variables

If you have headers and libraries installed in non-standard locations that cmake cannot find (e.g., fink on Mac OSX installs to /sw), then populate the environment variable CMAKE_PREFIX_PATH with a list of paths to these locations. When searching for headers, libraries and binaries, CMake will search all the paths in CMAKE_PREFIX_PATH as well as their standard subdirectories ("lib" for libraries, "include" for headers and "bin" for binaries). This can be also very useful e.g. if you install kdesupport to ~/install/kdesupport.

  • CMAKE_PREFIX_PATH, eg. export CMAKE_PREFIX_PATH=/sw

If you need to further tweak the search behavior, you can additionally set the following environment variables in order to add library, include or binary directories to your search path:

  • CMAKE_INCLUDE_PATH, eg. export CMAKE_PREFIX_PATH=/sw/include
  • CMAKE_LIBRARY_PATH, eg. export CMAKE_LIBRARY_PATH=/sw/lib
  • CMAKE_PROGRAM_PATH, eg. export CMAKE_PROGRAM_PATH=/sw/bin

For more information on variables, see this cmake.org wiki page

Going Further

If cmake finishes with "Generating done" then there was no errors, but if it finishes with "Configuring done" then there was errors that you have to fix. Once cmake finishes successfully, run your buildtool (i.e. make, KDevelop, XCode or MSVC) and build and wait until it has finished. Then "make install".

If you got a failure that says something like

CMake Error: This project requires some variables to be set,
and cmake can not find them.
Please set the following variables:
X11_XTest_LIB (ADVANCED)

then you may have a missing library (or other dependency). To find out which library, search in the cmake/modules directory for the variable that cmake can't find. In the example above, it is

find_library(X11_XTest_LIB Xtst ${X11_LIB_SEARCH_PATH})

So the missing library is Xtst. You then need to find it (perhaps installing a libXtst-devel library) and re-run cmake.

Using CMake for a Simple Application

Here's the most simple CMakeLists.txt:

add_executable(hello main.cpp)

This will create an executable named "hello" (or "hello.exe" under Windows) from the source file main.cpp. You can mix C and C++ files as you want. You can have multiple executables and libraries in one CMakeLists.txt. The same source file can be used in multiple targets, it will be compiled for each target independently from the other targets. Probably the most important part of the cmake language are the variables:

set( MY_SOURCES main.cpp widget.cpp)
message(STATUS "my sources: ${MY_SOURCES}")

So, use the SET() command to set the value of a variable. If you list more than one string, the variable will be a list. A list is a list of strings separated by semicolons. If you set it to only one item, it will have just that value. To get the value of a variable, use ${VAR}. You can iterate over a list using FOREACH():

foreach(next_ITEM ${MY_SOURCES})
   message(STATUS "next item: ${next_ITEM}")
endforeach(next_ITEM ${MY_SOURCES})

The commands in CMake are case-insensitive. Names of variables and names of parameter are case-sensitive.

You can also test for various things:

if (UNIX)
   message(STATUS "This is UNIX (including OS X and CygWin)")
endif (UNIX)

if (MSVC)
   set(MY_SRCS ${MY_SRCS} winextra.cpp)
endif (MSVC)

In this second example you can see also how to append items to a list.

In the cmake Wiki there is also a tutorial on using cmake to build KDE 4 software. It is recommended reading.

Using CMake for a KDE Project

Here's a basic CMakeList file that builds a small KDE 4 project:

project( kde4project )
find_package( KDE4 REQUIRED )
include(KDE4Defaults)

include_directories( ${KDE4_INCLUDES} )

set( KDE4ProjectSources kde4mainapp.cpp someclass.cpp someotherclass.cpp )

kde4_add_executable( kde4project ${KDE4ProjectSources} )

target_link_libraries( kde4project ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )

install( TARGETS kde4project  ${INSTALL_TARGETS_DEFAULT_ARGS} )

target_link_libraries contains the development libraries that are linked to your program. E.g. if you want to link to libtidy-devel, your library file name may be called /usr/local/lib/libtidy.a. You would then add -ltidy to a gcc call. Here, you add tidy to your target_link_libraries. If possible, use pre-defined variables or macros like ${KDE4_KDEUI_LIBS}.

install (TARGETS is where the target will finally be installed. If you do not have this line, make install will not be available.

Variables, macros and other useful information specific to KDE can be found at the CMake addons for KDE page.

Extending CMake

CMake can be extended using cmake scripts. CMake comes with a number of scripts; under UNIX they are by default installed to /usr/local/share/CMake/Modules/. The KDE libraries install also a set of cmake modules into share/apps/cmake/modules/. The files located there will be preferred over the ones in the system global cmake module path. For detecting software packages there are FindFOO.cmake files, see here for more information. You can also write macros in CMake. They are powerful enough to do most things you will need to build software, but they are not intended to be used as a general purpose programming language.

Converting autotools-based KDE software to CMake

In kdesdk/cmake/ you can find a script am2cmake . This is a ruby script, so you need to have ruby installed. Run am2cmake in the toplevel directory of your sources:

$ cd src/mykooltool/
$ am2cmake --kde4

Don't forget the switch --kde4, otherwise it won't generate files suitable for KDE 4 software. The converted files 'may' work as they are, but complicated projects will require some additional editing.

You may have to:

  • add more include direcories, using INCLUDE_DIRECTORIES()
  • add more link libraries, using TARGET_LINK_LIBRARIES()
  • add some compile switches, using ADD_DEFINITIONS()
  • add some "configure" checks, see How To Do Platform Checks and How To Find Installed Software
  • take special care of former libtool convenience libraries. They are not supported by cmake, instead there will be a file ConvenienceLibs.cmake created. In this file you will find for every convenience lib a variable, which contains all source files of this convenience lib. For the targets which linked to this convenience lib, just add the variable to the sources.
  • a file AdditionalInfo.txt will be created. There you will see all *.in and *.in.in files of your project. The stuff done in these files will have to be converted manually to cmake.