Using CMake build system: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Gather items into a category:Build.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:Build]]
 
== CMake with Qt4 ==
h1. CMake
 
CMake - the cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice.
CMake - the cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice.


You can use it instead qmake, native Qt buid system. In this article i describe base functionality, that usualy used to build simple project.
You can use it instead qmake, native Qt buid system. In this article i describe base functionality, that usualy used to build simple project.


===== qmake typical '''.pro file
This article show you to use cmake with Qt4. For Qt5 see here [http://doc.qt.io/qt-5/cmake-manual.html Qt5 CMake Manual]
===== qmake typical .pro file =====


<code>
<code>
TEMPLATE = app
TEMPLATE = app
SOURCES ''= main.cpp  mainwindow.cpp
SOURCES += main.cpp  mainwindow.cpp
HEADERS''= mainwindow.h
HEADERS+= mainwindow.h
FORMS ''= mainwindow.ui
FORMS += mainwindow.ui
RESOURCES''= main.qrc
RESOURCES+= main.qrc
</code>
</code>


As you can see, this project consists from 4 files. Three of them is implementing MainWindow class and one is main.cpp file.
As you can see, this project consists from 4 files. Three of them is implementing MainWindow class and one is main.cpp file.


h5. CMakeLists.txt for same project
===== CMakeLists.txt for same project =====
 
<code>cmake_minimum_required(VERSION 2.6)
<code>cmake_minimum_required(VERSION 2.6)
project ( PROJECT_NAME )
project ( PROJECT_NAME )
Line 66: Line 64:
</code>
</code>


You may use CMake, but most of people advice, not to use CMake for simple projects, because qmake build system functionality is more than enough for simple projects.
However, when you build and run in Windows or Mac OS X, you will get a console window that appears along with your GUI program. You may avoid this problem by appending below code to your CMakeLists.txt
<code>
# generate proper GUI program on specified platform
if(WIN32) # Check if we are on Windows
if(MSVC) # Check if we are using the Visual Studio compiler
set_target_properties(${PROJECT_NAME} PROPERTIES
WIN32_EXECUTABLE YES
LINK_FLAGS "/ENTRY:mainCRTStartup"
)
elseif(CMAKE_COMPILER_IS_GNUCXX)
# SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwindows") # Not tested
else()
message(SEND_ERROR "You are using an unsupported Windows compiler! (Not MSVC or GCC)")
endif(MSVC)
elseif(APPLE)
set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE YES
)
elseif(UNIX)
# Nothing special required
else()
message(SEND_ERROR "You are on an unsupported platform! (Not Win32, Mac OS X or Unix)")
endif(WIN32)
</code>
 
== Running moc/uic/rcc automatically ==
The AUTOMOC target property controls whether cmake inspects the C++ files in the target to determine if they require moc to be run, and to create rules to execute moc at the appropriate time.
 
The AUTOUIC target property controls whether cmake inspects the C++ files in the target to determine if they require uic to be run, and to create rules to execute uic at the appropriate time.
 
The AUTORCC target property controls whether cmake(1) creates rules to execute rcc at the appropriate time on source files which have the suffix .qrc.
 
Instead of using Qt meta-system (precompiler)--QT4_ADD_RESOURCES/QT4_WRAP_UI/ QT4_WRAP_CPP, you can simply add below code to your CMakeLists.txt:
<code>
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
</code>
 
A complete CMake-Qt example is posted on [https://github.com/kylinsage/using.cmake/tree/master/ch04-cmake-qt/spreadsheet3 GitHub]
 
You may use CMake, but  
most of people advice, not to use CMake for simple projects, because qmake build system functionality is more than enough for simple projects.  


h2. Project using CMake
== Project using CMake ==
''' KDE SC
* [http://www.kde.org KDE]
* Second Life =====
* Second Life  


== Links
== Links ==
* "Official Web-site":http://www.cmake.org/ ==
* [http://doc.qt.io/qt-5/cmake-manual.html Qt5 CMake Manual]
* [http://www.cmake.org/ Official cmake Web-site]
* [https://cmake.org/Wiki/CMake/Tutorials/Qt CMake/Tutorials/Qt]
* [https://cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html CMake Manual make-qt(7)]
* [https://github.com/kylinsage/using.cmake/tree/master/ch04-cmake-qt/spreadsheet3 GitHub CMake-Qt Example]
* [https://github.com/euler0/mini-cmake-qt GitHub CMake-Qt template]

Latest revision as of 15:17, 25 November 2016

CMake with Qt4

CMake - the cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice.

You can use it instead qmake, native Qt buid system. In this article i describe base functionality, that usualy used to build simple project.

This article show you to use cmake with Qt4. For Qt5 see here Qt5 CMake Manual

qmake typical .pro file
TEMPLATE = app
SOURCES += main.cpp  mainwindow.cpp
HEADERS+= mainwindow.h
FORMS += mainwindow.ui
RESOURCES+= main.qrc

As you can see, this project consists from 4 files. Three of them is implementing MainWindow class and one is main.cpp file.

CMakeLists.txt for same project
cmake_minimum_required(VERSION 2.6)
project ( PROJECT_NAME )

# declaring files in your project

set ( SOURCES
 main.cpp
 mainwindow.cpp
)

set ( MOC_HEADERS
 mainwindow.h
)

set ( UIS
 mainwindow.ui
)

set ( RESOURCES
 main.qrc
)

# Next lines needed for building all Qt projects
find_package( Qt4 REQUIRED )
include( ${QT_USE_FILE} )
add_definitions( ${QT_DEFINITIONS} )
include_directories( ${CMAKE_BINARY_DIR} )


# Next, using precompiler, compiler and linker

# using Qt meta-system (precompiler)
QT4_ADD_RESOURCES( RES_SOURCES ${RESOURCES} )
QT4_WRAP_UI( UI_HEADERS ${UIS} )
QT4_WRAP_CPP( MOC_SRCS ${MOC_HEADERS} )

# compile
add_executable( PROJECT_NAME ${SOURCES} ${MOC_SRCS} ${RES_SOURCES} ${UI_HEADERS} )
# or use line below instead, if you using Windows ™ Operating System.
#add_executable( PROJECT_NAME WIN32 ${SOURCES} ${MOC_SRCS} ${RES_SOURCES} ${UI_HEADERS} )

# build it (link libraries)
target_link_libraries( PROJECT_NAME ${QT_LIBRARIES} )

However, when you build and run in Windows or Mac OS X, you will get a console window that appears along with your GUI program. You may avoid this problem by appending below code to your CMakeLists.txt

# generate proper GUI program on specified platform
if(WIN32) # Check if we are on Windows
	if(MSVC) # Check if we are using the Visual Studio compiler
		set_target_properties(${PROJECT_NAME} PROPERTIES
			WIN32_EXECUTABLE YES
			LINK_FLAGS "/ENTRY:mainCRTStartup"
		)
	elseif(CMAKE_COMPILER_IS_GNUCXX)
			# SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwindows") # Not tested
	else()
		message(SEND_ERROR "You are using an unsupported Windows compiler! (Not MSVC or GCC)")
	endif(MSVC)
elseif(APPLE)
	set_target_properties(${PROJECT_NAME} PROPERTIES
			MACOSX_BUNDLE YES
	)
elseif(UNIX)
	# Nothing special required
else()
	message(SEND_ERROR "You are on an unsupported platform! (Not Win32, Mac OS X or Unix)")
endif(WIN32)

Running moc/uic/rcc automatically

The AUTOMOC target property controls whether cmake inspects the C++ files in the target to determine if they require moc to be run, and to create rules to execute moc at the appropriate time.

The AUTOUIC target property controls whether cmake inspects the C++ files in the target to determine if they require uic to be run, and to create rules to execute uic at the appropriate time.

The AUTORCC target property controls whether cmake(1) creates rules to execute rcc at the appropriate time on source files which have the suffix .qrc.

Instead of using Qt meta-system (precompiler)--QT4_ADD_RESOURCES/QT4_WRAP_UI/ QT4_WRAP_CPP, you can simply add below code to your CMakeLists.txt:

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

A complete CMake-Qt example is posted on GitHub

You may use CMake, but most of people advice, not to use CMake for simple projects, because qmake build system functionality is more than enough for simple projects.

Project using CMake

  • KDE
  • Second Life

Links