You are on page 1of 4

CMake cheatsheet | LeechCraft

http://leechcraft.org/development-cmake-cheatsheet

Search

About LeechCraft

News

Download

Plugins

Screenshots

Development

FAQ

Home Development Writing plugins About LeechCraft News Download Plugins Screenshots Development General Writing plugins Getting started Code style Architecture CMake cheatsheet Writing a simple plugin Having tabs Plugins for plugins Text completion Using notifications API docs Advanced Notifications Plugin-specific FAQ This cheatsheet contains solutions for typical tasks that a LeechCraft developer may face. We will mostly use code snippets from the CMakeLists.txt of the Auscrie plugin, which is listed fully at the end of the page.

CMake cheatsheet
Submitted by 0xd34df00d on Fri, 04/01/2011 - 01:40 Posted in development

Table of contents [hide] 1. Adding new source files 2. Handling .ui files 3. Adding resources 4. Adding translations 5. Installing arbitrary files 6. Adding Qt modules besides Core and GUI 7. CMakeLists.txt from the Auscrie plugin

Adding new source files


Add the names of the files you want to add to the corresponding variables, that is, SRCS and HEADERS in the ones generated by default by our genplugin.py script. So, for example, if you had SET (SRCS auscrie.cpp shooterdialog.cpp poster.cpp ) SET (HEADERS auscrie.h shooterdialog.h poster.h )

Contact us Our team Issue tracker Friends and you've written a class, say, RequestBuilder with requestbuilder.h and requestbuilder.cpp header/implementation files, you should have: SET (SRCS auscrie.cpp shooterdialog.cpp poster.cpp requestbuilder.cpp ) SET (HEADERS auscrie.h shooterdialog.h poster.h requestbuilder.h )

User login

Username: *

Password: *

Log in
Log in using OpenID Create new account Request new password

Please note that you should pass the headers to the QT4_WRAP_CPP CMake macro for the moc'ed files to be generated, which is absolutely necessary for QObjects to work. For example, with the following command:

Languages English

QT4_WRAP_CPP (MOC_SRCS ${HEADERS})

the MOC_SRCS variable would contain the list of sources generated by the moc. It should be

1 of 4

8/22/2011 5:11 PM

CMake cheatsheet | LeechCraft

http://leechcraft.org/development-cmake-cheatsheet

added as the dependency to your library, for example: ADD_LIBRARY (leechcraft_auscrie SHARED ... ${MOC_SRCS} ... )

Syndicate

Handling .ui files


Set a variable with the list of .ui files like the ones with headers and sources and pass it to the QT4_WRAP_UI command. For example, for two .ui files shooterdialog.ui and savedialog.ui, the following code would be written: SET (FORMS shooterdialog.ui savedialog.ui ) QT4_WRAP_UI (UIS_H ${FORMS}) After that, the UIS_H would contain the list of headers generated by the uic. It should be included as the dependency for your library: ADD_LIBRARY (leechcraft_auscrie SHARED ... ${UIS_H} ... )

Adding resources
To add the resources file, say, anheroresources.qrc, add the following to the CMakeLists.txt, before the ADD_LIBRARY call: SET (RESOURCES auscrieresources.qrc) QT4_ADD_RESOURCES (RCCS ${RESOURCES})

Don't forget to add the ${RCCS} to the list of dependencies for your library. For the above example with AnHero: ADD_LIBRARY (leechcraft_auscrie SHARED ... ${RCCS} ... )

Adding translations
Use the CreateTrVars macro defined in LeechCraft cmake files, like: CreateTrVars ("auscrie" "de;en;es;fr;it;oc;ru_RU;uk_UA" TRANSLATIONS CO

Here you pass the name of your plugin as the first parameter, the ;-separated list of translations languages as the second one and the variable names for the list of files with translations (*.ts) and compiled translations (*.qm) as third and forth ones respectively. Then, you should add a custom command for actually running the lrelease tool: ADD_CUSTOM_COMMAND (OUTPUT ${COMPILED_TRANSLATIONS} COMMAND "${QT_LRELEASE_EXECUTABLE}" ${TRANSLATIONS} DEPENDS ${TRANSLATIONS} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) Don't forget to add the compiled translations as the dependency of your plugin: ADD_LIBRARY (leechcraft_auscrie SHARED ... ${COMPILED_TRANSLATIONS} ...

2 of 4

8/22/2011 5:11 PM

CMake cheatsheet | LeechCraft

http://leechcraft.org/development-cmake-cheatsheet

) Don't also forget to install the resulting files if you don't embed them into resources (and this is the recommended scheme). See the next section regarding installing.

Installing arbitrary files


Just use the INSTALL macro. To install a list of files, for example, the compiled translations from the previous section: INSTALL (FILES ${COMPILED_TRANSLATIONS} DESTINATION ${LC_TRANSLATIONS_D Of course, you can hardcode the installation path instead of using variables. For that, just replace the ${LC_TRANSLATIONS_DEST} with the corresponding string.

Adding Qt modules besides Core and GUI


To include support for other Qt modules but Core and GUI, set the corresponding QT_USE_* variable before the line with INCLUDE (${QT_USE_FILE}). The full list of the variables is given at the CMake doc page. For example, for a plugin that needs QtNetwork as well: SET (QT_USE_QTNETWORK TRUE) INCLUDE (${QT_USE_FILE})

CMakeLists.txt from the Auscrie plugin


IF (NOT QT_USE_FILE) CMAKE_MINIMUM_REQUIRED (VERSION 2.6) IF (COMMAND cmake_policy) cmake_policy (SET CMP0003 NEW) ENDIF (COMMAND cmake_policy) PROJECT (leechcraft_auscrie) IF (NOT CMAKE_MODULE_PATH) SET (CMAKE_MODULE_PATH "/usr/local/share/leechcraft/cma ENDIF (NOT CMAKE_MODULE_PATH) FIND_PACKAGE (Boost REQUIRED) FIND_PACKAGE (Qt4 REQUIRED) FIND_PACKAGE (LeechCraft REQUIRED) ENDIF (NOT QT_USE_FILE) SET (QT_USE_QTNETWORK TRUE) INCLUDE (${QT_USE_FILE}) INCLUDE_DIRECTORIES ( ${CMAKE_CURRENT_BINARY_DIR} ${Boost_INCLUDE_DIR} ${LEECHCRAFT_INCLUDE_DIR} ) SET (SRCS auscrie.cpp shooterdialog.cpp poster.cpp requestbuilder.cpp ) SET (HEADERS auscrie.h shooterdialog.h poster.h requestbuilder.h ) SET (FORMS shooterdialog.ui ) SET (RESOURCES auscrieresources.qrc) CreateTrVars ("auscrie" "de;en;es;fr;it;oc;ru_RU;uk_UA" TRANSLATIONS CO QT4_WRAP_CPP (MOC_SRCS ${HEADERS}) QT4_WRAP_UI (UIS_H ${FORMS}) QT4_ADD_RESOURCES (RCCS ${RESOURCES}) ADD_CUSTOM_COMMAND (OUTPUT

3 of 4

8/22/2011 5:11 PM

CMake cheatsheet | LeechCraft

http://leechcraft.org/development-cmake-cheatsheet

${COMPILED_TRANSLATIONS} COMMAND "${QT_LRELEASE_EXECUTABLE}" ${TRANSLATIONS} DEPENDS ${TRANSLATIONS} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) ADD_LIBRARY (leechcraft_auscrie SHARED ${COMPILED_TRANSLATIONS} ${SRCS} ${MOC_SRCS} ${UIS_H} ${RCCS} ) TARGET_LINK_LIBRARIES (leechcraft_auscrie ${QT_LIBRARIES} ${LEECHCRAFT_LIBRARIES} ) INSTALL (TARGETS leechcraft_auscrie DESTINATION ${LC_PLUGINS_DEST}) INSTALL (FILES ${COMPILED_TRANSLATIONS} DESTINATION ${LC_TRANSLATIONS_D LeechCraft architecture up Writing a simple plugin in 20 minutes Login or register to post comments

Hosted by 0xd34df00d.me.

4 of 4

8/22/2011 5:11 PM