How to automate Qt moc? - c++

I have to run the following commands from Qt command prompt: qmake -project then make and this gives me the debug folder with the Moc file.
This is strangely the only way my PC will generate the moc_.cpp file.
So how can I automate the task of these commands so I don't have to use these commands again?

You should not run qmake -project multiple times. The -project option is meant to provide you a template project file for you to edit. An equivalent of what you are doing in an IDE would be creating a "New Project" every time you want to build. After you have the initial project, you should edit it manually, add files when you have new files, etc. If some header file changes, the generated Makefile will notice it and call moc on it to update the moc_*.cpp file automatically. So:
Run qmake -project when you start working on a project.
Run qmake when you want to generate Makefiles.
Run make when you want to build the project.

I guess you have two choices.
call qmake from a parent make process and do a multilevel build. ("Recursive make".)
directly run the meta-object compiler from rules in your makefile
If the second, this page on using the meta-object compiler may help.

Related

Can a project created with qmake be compiled without using qmake?

I have some source code that was built with qmake. Compiling it is supposed to be done with qmake qmakefile.pro.
I'd like to compile it without using qmake. Is that even possible? What would the steps be?
Edit: Here's the .pro file.
Yes, you can compile files without qmake.
You need to get any specific compiler settings from qmake and convert them to the other compiler as necessary.
Or you can convert the contents of the qmake.pro file into a makefile or your IDE's project settings.
If the qmake.pro is not a human readable text file, you will have to launch qmake and retrieve the settings. Otherwise search for a conversion tool.
While it is technically possible to translate the .pro file into something else, I would not advise it.
Why?
QMake does a lot of work behind the scenes.
In the case of the .pro file you referenced (http://pastebin.com/viv204Fv), QMake is invoking meta-tools (uic, moc) to create the code for the UI and adding build rules to compile it. You will need to manually code those in your build script.
-. QMake also solves issues of choosing the right compiler and linker options (including the right include files). You will need to determine them yourself and add them to your build script.
The project is already using Qt, so why not use Qt's build script generating tool, QMake? If you have installed a Qt development environment, you should already have qmake installed.

Using Qt with eclipse, #include <QSqlDatabase> leads to file not found

So I have started a new Qt console project;
everything so far has been working fine. I have eclipse set up with the integration, but i'm not actually using it, i have just included the relevant Qt directories so that my #include <QtGui> calls work.
These have all worked perfectly so far.
i can compile it either using eclipse's build option, or by doing
qmake -project
qmake
make
from the command line. Both work.
the include statements i have; are:
#include <QtGui>
#include <QApplication>
In two different files.
If i add anotherone though, i.e. #include <QSqlDatabase> then eclipse recognises the include, and lets me initialise new variables, and use the QSqlDatabase classes. Everything seems fine - see here - eclipse screenshot
When i build the project though, it decides it can't see the files even though they are all there, see them here in the terminal too. And then eclipse puts red lines everywhere.
If i go into the directory, and attempt to do it all via the terminal, using the qmake -project; qmake; make then i get the same errors.
If however, I go into the .pro file created by qmake -project, and add the line QT += sql, and then do qmake; make, then all is fine and it works.
Why is qmake -project leaving out these files from the project file? I'd really rather not have to go in and manually add that line every time I build it...
Why is qmake -project leaving out these files from the project file?
As far as I know, qmake isn't supposed to replace human programmer. If you want to know WHY it doesn't detect SQL dependency, you can read qmake source code.
I'd really rather not have to go in and manually add that line every time I build it...
It sounds like you're trying to do something weird here. You don't need to call qmake -project before every build.
qmake -project generates initial project - *pro file. Once initial project has been generated, you can tweak it, and add options or some macro magic if you want. Then, using *.pro file, you generate script for your build system (vcproj for windows, makefiles for windows/*nix, nmake make files, etc).
Workflow goes like this:
qmake -project.
Repeat forever:
2.1. modify generated *.pro file to your liking.
2.2 qmake
2.3 Build using your build system. (nmake, make, visual studio, etc.)
To simply "build" all you have to do is #2.3. You call qmake (without -project) switch only if you modified *.pro file.
See qt documentation and tutorials for more info.

QtCreator and "Run CMake"

When running CMake from the project view in QtCreator ("Run CMake" command), CMake wizard pops up. This is becoming really annoying and since I'm QtCreator newbie, I don't know if there is any remedy for this.
Desired effect would be automatic execution of cmake, without the CMake wizard pestilence.
Yes, this GUI annoying me too.
The only option is to add custom build step in your Project configuration
run app: cmake
params CMakeLists.txt
working folder: %{sourceDir}
this step should be added BEFORE make step.
Once you have created a project in Qt Creator, you do not need to use the "Run CMake" command unless for some reason you think the project files are out of date. Just click build, run, debug etc and it will take care of compiling the files that need to be compiled and invoking CMake as and when necessary.
Underneath it uses Unix Makefiles with Codeblocks in order to generate an XML description of the project. Running CMake from the menu explicitly re-runs CMake and reparses the XML description.

Qt - qmake -project, why is it stuck?

I'm trying to execute the qmake -project command by doing:
C:\Qt\2010.05\qt>qmake -project
But, it remains there and doesn't get executed. Why is that?
Thanks.
qmake -project without any additional arguments will traverse the current directory recursively, looking for e.g. source, header and UI files to be included in the .pro file. When you run it in a directory containing lots of files (such as Qt folder), it can take a lot of time to go through all of them. Thats why it seems to get stuck.

Eclipse CDT build/run on file basis

In my scenario I have a C++ project in CDT Eclipse. This projects however is rather a collection of individual (helper) programs than one complex application. Consequently I want to be able to build and run them individually.
My project structure is very simple and looks like:
src/app1.cpp
src/app2.cpp
src/...
Note that I do not have common header files or libraries. However I want to be able to add programs to this project just by creating e.g. src/appx.cpp
Ideally I want to have shortcuts for
"Build currently opened .cpp"
"Run binary of currently opened .cpp"
Any suggestions on how to achieve this behaviour, if possible without additional plugins?
The straightforward way to succeed what you aim is to create a Makefile project with CDT and add a new target rule for each of your applications inside your Makefile. You can even use SCons or other build systems with a CDT Makefile project and obtain the same effect.
You can also trick the managed build to create executables instead of object files. Remove -c option from Other flags of C++ compiler settings inside project properties. This will produce a separate application file for each of your source files.
Application files which are created inside the build directory will have the object file extension and they will not be executable. To solve this, you can add a post build script in your project directory such as:
postbuild.sh for Linux:
chmod +x *.o
rename -v 's/\.o$//' *.o
or postbuild.bat for Windows:
rename *.o *.exe
After adding ../postbuild.sh or ../postbuild.bat as a post build command in your build settings, you applications will be ready to run. Right click on any of these executable files and choose Debug As or Run As and a new Run configuration will be created.
Also you will have to stop the linker of the managed build to prevent errors. This can be achieved with changing the linker command to true (Linux) or true.exe (Windows, msys).