Adding custom target in qmake - qt

I want to build my resources with qmake as follows [Qt 5.5]:
imageTarget.target = images.rcc
imageTarget.depends = $$PWD/images.qrc
imageTarget.commands = rcc -binary -no-compress $$PWD/images.qrc -o $$OUT_PWD/images.rcc
QMAKE_EXTRA_TARGETS += imageTarget
When I run qmake for my .pro file, it generates the make rule for target images.rcc target as expected:
images.rcc: /path/to/images.qrc
rcc -binary -no-compress /path/to/images.qrc -o /output/path/to/images.rcc
So far so good.
However, what I would expect is that running qmake would also generate the output file images.rcc and it does not.
But when I go into the makefile directory and type in the command "make images.rcc", then the images.rcc is generated. Am I missing a point? How can I make target in the qmake step without the need of extra make?

With
QMAKE_EXTRA_TARGETS += imageTarget
you just define a new target - but it is not automatically built when running make.
Try to add
PRE_TARGETDEPS += images.rcc
This should automatically build a new images.rcc when running make if images.qrc has changed.

Related

Makefile made by qmake only works on my pc

For a university project we made an OpenGL application which uses Qt for the GUI. When I use qmake -spec macx-g++ project.pro I can make a Makefile, and if I then do make it correctly makes the application. However, when I then send the complete folder to the other person in the project and he does make he gets the error
Makefile:209: warning: overriding commands for target `moc_window.cpp'
Makefile:203: warning: ignoring old commands for target `moc_window.cpp'
make: *** No rule to make target `/usr/lib64/qt-3.3/mkspecs/default/qmake.conf', needed by `Makefile'. Stop.
However, when in the same folder he does qmake; make it does work correctly. The problem is that when handing the code in we don't know if the professor has qmake available, so we would like it to work by only using make. Are we missing something that should be addded to have the Makefile made with qmake work, without having to do qmake again? Our .pro file is below.
TEMPLATE = app
TARGET = smoke
QT = core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += \
fluids.cpp \
simulation.cpp \
visualization.cpp \
window.cpp \
myglwidget.cpp \
vector.cpp \
grid.cpp \
scalar.cpp
HEADERS += \
window.h \
myglwidget.h \
simulation.h \
vector.h \
grid.h \
visualization.h \
scalar.h
INCLUDEPATH += fftw-2.1.5/include /usr/local/include include
LIBS += -L"$$_PRO_FILE_PWD_/fftw-2.1.5/lib" -lrfftw -lfftw -framework OpenGL -framework GLUT -stdlib=libc++
QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_CXXFLAGS += -std=c++11
FORMS += \
window.ui
The build directory is not portable. It is only ever meant to work on the system where you executed qmake, and for the particular Qt install that you ran qmake from. Recall that each Qt install has its own qmake, so e.g. if you have three Qt versions, each provides its own qmake that you must use to build a project using that version.
The project you ship should contain the .pro file, the sources, and whatever else the build calls for, like icon files, resources, etc.
The recipient, to build it, should:
Create a build folder.
cd into the build folder.
qmake /path/to/sources/project.pro && make -j
That's how you distribute and build Qt projects.
why does qmake not build the application directly?
Why do you not make your own shoes? The reasons are the same. Don't reinvent the wheel.
Of course, engineers sometimes have an itch to scratch. Enter Qt Build System (qbs). If your project's build is given as a .qbs file, then you do:
Create a build root folder.
cd into the build root folder.
qbs -f /path/to/sources/project.qbs - this builds the project.

How to put gstab+ option to mingw makefile generated by qmake

I am newby in Qt, and try to compile tutorial application with MinGW. The only problem, that I need to put -gstab+ option to gcc compiler instead of -g option that qmake generated by default.
Currently, I select debug configuration (it add CONFIG += debug to the qmake command-line).
I've tried to add CONFIG += gstab+ to the .pro file, but it does not work. qmake generates makefile.Debug file with -g option.
What do I make wrong? And how should it be?
I've found a solution here http://qt-project.org/forums/viewthread/2412
I've added the following lines to my .pro file:
QMAKE_CXXFLAGS_DEBUG += "-gstabs+"
QMAKE_CFLAGS_DEBUG += "-gstabs+"

QMake generating weird paths in makefile

I'm using Qt 5.1.1 and running qmake on windows.
I run qmake with the following command:
qmake.exe -spec win32-msvc2012 -tp vc project.pro
Somehow in my makefile it generates some weird relative paths:
INCPATH = -I"..\..\..\qt\qwt-6.1.0\src"
for example.
My includepaths in the .pro file are the following:
INCLUDEPATH += \
$$PWD \
$$QWTDIR \
what did I do wrong? (The compiler searches for ......\ which it isn't allowed to access for sure)
qwtdir is defined as:
QWTDIR = C:/qt/qwt-6.1.0/src
I resolved the error. Someone committed corrupted .pri file which didn't contain a proper line break after an include i.e.:
HEADERS += \
$$PWD/file1.h \ $$PWD/file2.h
Adding a proper line break solved the issue.
INCLUDEPATH += $$PWD is most likely unnecessary.
Your $$QWTDIR is relative, most likely - you'd need to relent and show it to us. Use $$absolute_path($${QWTDIR}).
You could also probably put the include paths all on one line. The trailing line continuation in the last line of INCLUDEPATH is wrong, you must remove it:
INCLUDEPATH += \
$$PWD \
$$QWTDIR
You are trying to build against the source tree of Qwt ( probably copying the project files of the Qwt examples ) instead of installing Qwt properly and building against the installed version using:
CONFIG += qwt
See http://qwt.sourceforge.net/qwtinstall.html

Qt pro file call another makefile

Is there a way to use the .pro file in Qt to call another Makefile? For example, in a makefile, you could use:
make -f /other/path/Makefile
Is there anything like using the pro file for Qt?
Something like this should work:
QMAKE_EXTRA_TARGETS += other
PRE_TARGETDEPS += other
other.commands = make -f /other/path/Makefile
this will cause make -f /other/path/Makefile to be called as part of the make process, and will also give you the ability to type make other to just run that command.

Running a program/script from QMake

We have a fairly large code-base. The vast majority of the code is compiled using qmake to produce the makefiles. However, there are some sub-projects that get produced by running batch files or running other programs.
I'd like to be able to have everything compiled using qmake, but I can't figure out how to get qmake to simply run a script.
One thing that I've tried is using QMAKE_EXTRA_TARGETS in my pro file, like so:
TEMPLATE = lib
SOURCES = placeholder.cpp
CONFIG += no_link staticlib
batch_runner.target = placeholder.cpp
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS = batch_runner
I then have to have the batch file produce placeholder.cpp like so:
# do the real work here
# ...
# create placeholder.cpp so qmake and nmake are happy
echo // dummy >> placeholder.cpp
This seems to work fine. The trouble is that it is somewhat hokey. If I don't specify batch_runner.target (i.e. I leave it blank) or don't put placeholder.cpp in SOURCES then the batch file never gets run. This is because qmake isn't making batch_runner.commands the action for any other dependency in the Makefile.
Is there any better way to get QMake to construct a Makefile such that a script is run when the Makefile executes?
It looks like QMAKE_POST_LINK works well for this sort of thing.
This seems to get the job done. my_batch_file.bat runs when nmake runs (rather than when qmake runs) and I don't need to do anything funny with placeholder targets or files.
It's quite likely that I don't need all of the items listed in 'CONFIG'.
TEMPLATE = lib
TARGET =
CONFIG += no_link target_predeps staticlib
QMAKE_POST_LINK = my_batch_file.bat
QMAKE_CLEAN += batch_output.obj
Try the system() command. For example:
system(pwd)
Here is another solution:
TEMPLATE = aux
OBJECTS_DIR = ./
DESTDIR = ./
first.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS += first
QMAKE_CLEAN += batch_output.obj
The template aux basically produces a makefile which does nothing when run without specifying a target. The OBJECTS_DIR and DESTDIR variables are set to the current directory to prevent that qmake creates the debug and release directories (it's important to set them to ./ and not just to .; at least on Windows). Then, using QMAKE_EXTRA_TARGETS, we redefine the target first to run the custom command when the makefile is invoked without target.
It's a bit hacky but it gets the job done.
Addition:
If you want to prevent the generation of three makefiles (Makefile, Makefile.Debug, Makefile.Release), you can add
CONFIG -= debug_and_release
However, if you use this and depending on how the makefile is invoked (always invoked manually, invoked by parent directory's "subdirs" *.pro file, ...), it might be necessary to create fake debug and release targets to avoid "no rule to make target..." errors. For example:
release.target = release
release-clean.target = release-clean
release-install.target = release-install
[...]
debug.target = debug
debug-clean.target = debug-clean
debug-install.target = debug-install
[...]
QMAKE_EXTRA_TARGETS += release release-clean release-install [...]
QMAKE_EXTRA_TARGETS += debug debug-clean debug-install [...]
You could use the SUBDIRS configuration to run multiple different targets, even from the same makefile. This might work especially well with your extra targets, as a subdir configuration can specific a specific target in the makefile to run (see undocumented qmake for details). In this case, I would put all of the "regular" build commands in one .pro file, the external build commands in another, and a subdirs .pro file to build all of them. I haven't tested anything quite like this, but it should work.
regular.pro:
SOURCES += main.cpp
TARGET = regular.exe
external.pro:
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS += batch_runner
other_runner.commands = other_batch_file.bat
QMAKE_EXTRA_TARGETS += other_runner
do_it_all.pro:
TEMPLATE = subdirs
CONFIG += ordered
regular.file = regular.pro
SUBDIRS += regular
batch.file = external.pro
batch.target = batch_runner
SUBDIRS += batch
other.file = external.pro
other.target = other_runner
SUBDIRS += other

Resources