Building Projects with CMake and Qt Tutorial

Here is a real simple example of how to build a basic project with CMake and Qt. Hopefully this should be enough to get you started. In Qt just click open project and select the CMakeLists.txt file. Select the build folder and you are done. The code structure below works with any source files in the same folder as your CMakeLists.txt file. I set it up to build to a Debug folder in the parent directory/folder of the CMakeLists.txt file.

This CMake file will build my ModernTriangle example of how to use Qt 5.5 with modern OpenGL. Skip to the bottom of this post for some more info on why you might want to use CMake with Qt Creator.


project(ModernTriangle)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/../Debug)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../Debug)

#setup for qt packages
#http://stackoverflow.com/questions/14115024/how-to-link-qtmain-in-cmake-with-qt5
#http://www.kdab.com/using-cmake-with-qt-5/
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Gui)

SET(Main_SRC
./main.cpp
./main.h
./openglwindow.cpp
./openglwindow.h
)
SOURCE_GROUP(Main FILES ${Main_SRC})

INCLUDE_DIRECTORIES(${ModernTriangle_SOURCE_DIR})

IF(MSVC)
# /FS is to use jom calling multiple cl threads
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG_INIT} -D_DEBUG -D_DRAWGI /Zi /W4 /FS")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE_INIT} -D_SECURE_SCL=0 /W4 /FS")
ELSE()
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG_INIT} -D_DEBUG -D_DRAWGI -Wno-deprecated-declarations")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE_INIT} -D_SECURE_SCL=0 -Wno-deprecated-declarations")
ENDIF()

IF(MSVC)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS_INIT} /ENTRY:mainCRTStartup")
ENDIF()

ADD_EXECUTABLE(ModernTriangle WIN32
${Main_SRC}
)

TARGET_LINK_LIBRARIES(ModernTriangle
Qt5::Gui
)

Qt Creator is a great cross platform tool. It defaults to using qmake as its build tool. There are a few common reasons why people choose not to use qmake.
-If your project is complex and needs a different feature set than qmake
-If you want the flexibility to build for a different IDE someday or concurrently

Whoa there cowboy, I just started this whole programming thing! What exactly does building mean? Well, a build is basically the whole process to generate a program or library from source code. It often includes preparing an IDE project or solution, generating compile and linking instructions and sometimes checking in source code to a version control system. You can build a single program on one operating system or even build multiple versions of a program for many different operating systems all at once. While none of these tasks can’t be completed manually, it could become so much work that you wouldn’t get to spend anytime coding without different build tools.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s