Maintaining CMake on Qt Creator - qt

I wonder, how do people maintain CMake projects on Qt creator? When you open the project you run CMake generator and after that you can't change much. In order to add another class to the project you have to create class files by hand, add them to CMakeLists.txt and rerun CMake again. Moreover, changing the class name is a nightmare.
I do actually like CMake (except its syntax), since it has some nice features. However, maintaining a project is a nightmare. Am I missing or doing something wrong?

No, you did not, that is how cmake works: You maintain the build system by hand and generate files for your preferred build system.
That approach makes it pretty easy to support several IDEs/buildsystems using cmake. Unfortunately it also makes it impossible to have an Integrated Development Environment for cmake projects. The rather complex syntax which makes it basically impossible for a machine to modify cmake projects does not help there either.

Related

Build application and Qt from source using cmake

I'm trying to set up building an application that uses qt5.6 in a way that both qt and the application are build from source (using the ninja generator, with visual studio compiler on windows and clang on mac).
I'm stuck at find_package(Qt5Core ..) : when Qt is not build yet, it will not be found. And because it's not found, the generate cmake file is not complete.
I think I need a setup where it generates a ninja files that, when build, builds Qt and then regenerates the ninja file (and at this point it would find qt) before continuing the build.
Or any other way in which I can build Qt+application from source, so that if I change something in Qt, it is automatically rebuild.
How should I set up my cmake file(s) to do that?
You could use CMake's ExternalProject command to invoke CMake from within CMake. You can specify dependencies there, so that your application will only be built after Qt has been built.
I happend to have a small example here that uses ExternalProject_Add to build a library followed by an application. In that example, CMake for the library and the application is invokved at make time.
cmake_minimum_required(VERSION 3.0)
include(ExternalProject)
ExternalProject_Add(cmake_lib
URL ../cmake_lib
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
)
ExternalProject_Add(cmake_app
DEPENDS cmake_lib
URL ../cmake_app
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
)
The problem with Qt 5 is that it doesn't use cmake to get built, and thus your ninja build process won't know anything about the internals of Qt unless you tell it everything. E.g. if you change any dependencies of Qt, or if Qt gets partially built, you'd have to add a lot of knowledge to your project's build system to determine whether the Qt build needs to be invoked again. This can be certainly made to work in a "fire and forget" style of build, where the build always starts from scratch, like in a CI system. One quickly runs into serious trouble if the intent is to change Qt itself and re-run the build. Even Qt's own build system has serious trouble with full dependency tracking due to fundamental architectural decisions in qmake.
Qmake-based developer re-builds of Qt suck - and they do to such an extent that I didn't bother contributing to the project since it felt like a penance, where the simplest of changes to one source file would take a minute or more to rebuild on, and any changes to qmake project files would sometimes ballon into multi-minute affairs.
The only solution that I have found that actually works and doesn't make you hate life, the universe and everything, was to reimplement Qt's build and configuration system using cmake, so that qmake becomes unnecessary. As a result, a statically linked unity debug build of qtbase takes a couple of minutes with all features enabled - it's pretty zippy and is much faster than the fastest option provided by Qt 5 on Windows for MSVC builds: qmake+jom.
The Qt project endeavored to do this during Qt 6 development, and I believe that they were successful :)

Setting QTDIR based on generator in use

I am trying to setup a CMake project which consists of multiple static libraries and one main executable which has dependencies on QT. I would like to be able to select either a MinGW or a MSVC build when I run the cmake build.
I recently learned that the QT specific parts of CMake will find the installed library binaries for QT automatically if QTDIR is set as an environment variable and if QTDIR/bin is in the PATH.
Now I am wondering how I can set this information dynamically depending on the compiler that I am using. Since I would need to point cmake to either one of C:\Qt\5.3\mingw482_32 or C:\Qt\5.3\msvc2013_64, or possibly others if I extend the list of targets in the future.
Note that I do not need to build both compiler targets at the same time, I would usually select on target in my IDE and build that one, but I would like to be able to switch between both targets.
The solution should be in such a way, that on Linux the CMake build will still find the libraries which are installed through the package manager automatically.
Can I easily achieve this with CMake?
As a side note, in the future I might also need to select the QT version on demand? So an extensible solution would be nice.

What are CMake vs qmake pros and cons?

I would like to know reasons for use CMake for particular project over qmake and vice versa.
Simply what are the pros and cons of both build systems?
Both are build systems, but they're not very similar at all. If your project uses Qt, you're probably best off using qmake. CMake is more generic, and fits pretty much any type of project.
Both qmake and CMake generate a Makefile, which is read by make to build the project. Not all build systems generate a Makefile, but many do. Even a Makefile is a type of build system; it tells the compiler and linker what to do, in order to create an executable (or a dynamic or static library).
If your project uses Qt, but you don't want to use qmake, you'll have to do a few more things yourself:
running the Meta Object Compiler (MOC)
include paths (tell the compiler where to look for Qt headers)
linking (tell the linker where to look for Qt libraries)
So, you'll have to do a bit more work to build a Qt project without qmake, but it is possible and it will teach you a lot about how Qt and qmake do things.
On a personal note (take this only as a recommendation, do further research yourself): I'm not a big fan of qmake. It helps you with the Qt stuff, but apart from that, I've found it to be pretty limited.
In any case, I would recommend learning to build a small project (~10 source files) without using any type of build system. Not using CMake, not with a Makefile, just using the compiler and linker directly. You shouldn't actually build any real project in this way, but you should learn how to do it, just to learn what build systems actually do. Knowing what they do will make them a lot easier to use.
A few months ago, we switched a project from qmake to Premake, which is also worth a look. It's highly scriptable (with Lua), which is great when you need to customize your build process.
That said, it is a bit more "manual", so prepare yourself to learn how compiling and linking works at a more basic level, without the use of a build system. It's also in beta (Premake 5), so there are some bits and pieces still missing.
You can also take a look at qbs, which is supposed to be a better qmake. It's still in a beta stage, so I'd wait while it matures and becomes easier to use.
CMake is by far the more powerful build system. The syntax is "not so nice" to put it mildly. But then, for any complex project, what one has to do with QMake (or any buildsystem I know of) to achieve things isn't nice either. For simple projects, QMake is nicer to look at though.
If you need configure checks for third-party dependencies other than Qt, CMake is what you want, support for configure checks in QMake is minimal to non-existent.
QMake on the other hand works great with Qt Creator (CMake support in there is lacking, although it's doable to use CMake with Creator).
If you want to build and deploy for iOS and Android from within Qt Creator, I strongly suggest QMake. (Not sure if it's even possible these days with CMake - it will be certainly cause a lot more headache).
I use CMake for my Qt projects and am very happy with it. Specifically, I have the following in my CMakeLists.txt:
set(QT_VERSION_REQ "5.2")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Core ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Quick ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Widgets ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Gui ${QT_VERSION_REQ} REQUIRED)
set(CMAKE_AUTOMOC ON)
QT5_WRAP_UI( UI_HDRS ${UI_FILES} )
ADD_EXECUTABLE(${MOC_HEADERS})
target_link_libraries(${PROJECT_NAME}
Qt5::Core
Qt5::Quick
Qt5::Widgets
Qt5::Gui
)
I hope this helps if you do decide to go with CMake.
CMake will help you generating configuration files for many build systems (a "build system" is called Generators in CMake). See What is a CMake generator?.
It means that if you have a set of C/C++ sources and a well written CMakeLists.txt, you can use CMake to create projects for different build system (IDE based on command-line based) like Visual Studio, CodeBlocks, g++...
With CMake, you can choose the IDE and compiler you will use in the end and 'easily' switch between supported ones.
qmake, as far as I know, will only support QtCreator as an IDE (itleft using 3rd party compiler in the background: nmake from Visual Studio, g++, MinGW...). But you won't generate a Visual Studio solution (sln file) using qmake. That's the main limitation I see in qmake (because I hate QtCreator...Visual Studio is way more powerful and intuitive...but that's just my opinion).
It's true qmake makes it easier to compile Qt-based applications (because MOC, .ui and liking to Qt is natively supported) than CMake. But it's doable anyway using CMake (there's built-in functions for Qt integration). Only your CMakeLists.txt may have to be reworked a bit when moving to new releases of Qt (I had a hard time when moving from Qt4 to Qt5, I guess that using qmake makes this easier).
Personnaly, I use CMake for my build environment and, when I need to use QtCreator (for Android deployment mainly), I make my CMake scripts generate a .pro file for qmake/QtCreator. It works well because qmake .pro file syntax is really simple. Then I'm not locked to a specific IDE.
qmake
Has focus on projects using Qt
Project file is easily generated by QtCreator (good for beginners)
Supported by QtCreator
CMake
Is used in a wide range of projects
Supports a lot of platforms and languages
Supported by multiple IDE's: e.g. QtCreator, Visual Studio
Generates project description for multiple IDE's
Contains commands to make usage of Qt easy (Most important: automoc)
My recommendation: Use qmake in case QtCreator is your IDE and you starts with Qt or C++. Use cmake in case you want to do any complex stuff in your build.
Both qmake and CMake work similary. See http://www.th-thielemann.de/development/cmake/cmake_qmake_to_cmake.html for a tutorial to migrate from qmake to CMake.

macdeployqt on homebrew installed frameworks

I'm trying to deploy an application using macdeployqt. All Qt frameworks get copied correctly into the application bundle. The problem I encounter is that macdeployqt does not have write permissions on the copied frameworks which originally reside in /usr/local/lib. This is because I have installed qt using homebrew which seems to make install everything read only. My question is whether there is a better way to fix this issue then manually changing all permissions of the qt libraries inside /usr/local/lib so that I can use macdeployqt from within a qt .pro project. (I don't want to use macdeployqt manually with sudo or such)
The reason why I'm asking is because I am using many third party libraries in the project (they get copied ok etc.) which I need to update often through homebrew and thus have to redo the permission changing on them.
Thanks in advance!
Just in case someone finds this old post looking for info about macdeployqt:
Use a script to do macdeployqt in preference to scripting the macdeployqt commands in your .pro file. That will allow you to change the permissions on the files on the fly.
Here is [a snippet of] the script I use for one of my apps:
https://bugreports.qt-project.org/browse/QTBUG-23268
If you're on Windows and don't have bash, you can use perl or python. The script referenced above modifies the files on the fly to work around a bug - you can put anything you want here, including changing the permissions on the files.
Using a script also means that you have the flexibility to add commands later to do code-signing, packaging or whatever else you need.
The Qt .pro "scripting language" actually generates Makefile commands under the hood and can be quite obscure if you want to accomplish deployment tasks that relate to paths and sets of files.
Also you'll need to create an extra target or include it into your build target - either way the build process becomes more complex and more error prone.
Disclaimer: I worked on Qt for 8 years as a Senior Engineer for Nokia/Trolltech, and also have published a commercial cross-platform app using Qt.

Qt create executable

Is there a quick, straightforward way to make a Qt application into an executable? I attempted to follow the instructions at http://doc.qt.digia.com/4.1/deployment-windows.html but have been unsuccesfull thus far; I'm unable to Any help would be much appreciated. Thanks!
I always use CMake to build Qt projects, it's easy, free and cross platform. Guide : Compiling Qt4 apps with CMake. CMake also come with CPack to easly make installer for Windows, Mac and Linux.
I agree with chmod700 about the Qt Creator suggestion, it's not my favorite IDE but it's still really nice and easy.
Do you mean an installer package? I assume you are able to compile, link, and run your app and you mean how do you package it up for others.
http://installbuilder.bitrock.com/ <-- special handling of Qt based projects but costs $
http://www.jrsoftware.org/isinfo.php <-- my personal favorite and it's free (can be rough to learn advanced features though)
Though if you mean how do you build your app, you may want to try the new Qt Creator (http://www.qtsoftware.com/products/appdev/developer-tools/developer-tools#qt-tools-at-a) which will setup build targets for you and really makes desktop Qt dev a snap. Also if you're still using 4.1, you can now use 4.5 under the LGPL on all platforms making it almost a no-brainer to upgrade.
I'm not sure if I understand your problem. Assuming you're using MinGW, it's really easy and quite straightforward:
get the Qt sources and unpack them to some folder (f.e. c:\Qt\4.5.0-static)
install MinGW. Make sure the MinGW executable folder is in the %PATH% variable.
open a cmd windows, go to the Qt sources and run configure -static. You can add other config options if you like to, but usually you don't need that.
when building Qt finished, go to your application sources, open a cmd window and run the qmake of your built Qt installation -- i.e. c:\Qt\4.5.0-static\bin\qmake in the example given above.
run make
you get a statically linked binary in the end (you might want to check it with Dependency Walker).
Doing the same using Visual Studio is pretty similar.
Or do you want to build dynamically and create an installer package?

Resources