1

I have a project hierarchy like this:

file.pro

src/
    files.cpp

include/
    headers.h

build/
    files.o

bin/
    finalExecutable

What to specify in the .pro file so that the Makefile generated by qmake takes the .cpp files from my src/ directory and put the executable in the build directory?

For the include part, I have added INCLUDEPATH += include/*, and it seems to work other than the fact that while including any file.h I have to write #include <include/file.h> instead of #include <file.h>. How to put it in the .pro file so that I can include headers like #include <file.h>.

I tried to add SOURCES += src/*, but it shows an error:

error: No rule to make target 'sourceFiles/*', needed by 'mydialog.o'.  Stop.

How to go about specifying the include, src and the build directories?

arpanmangal
  • 1,770
  • 1
  • 17
  • 34

2 Answers2

2

This should be easy. Just include this in your .pro file

MOC_DIR     = ./build/moc
OBJECTS_DIR = ./build/obj
RCC_DIR     = ./build/qrc
UI_DIR      = ./build/uic

and voilà, the folder build and its subdirectories are created as soon as you execute qmake.

Also when you have local includes, you should not use #include <localheader.h> it should be #include "localheader.h"

Marcus
  • 1,685
  • 1
  • 18
  • 33
  • The Build part worked perfectly fine. As for the include in my custom Makefile if I pass the flags `-I include/` then `g++` automatically takes in the headers from `include/` and I am able to write `#include `. It's very difficult and not so nice to include the absolute path in `""` with all the `../../` and other things. So how to do the same thing with a `.pro` file – arpanmangal Mar 14 '18 at 09:44
  • also how to specify the `src` directory? – arpanmangal Mar 14 '18 at 09:49
  • The code now accepts the `src/` after adding the build lines. – arpanmangal Mar 14 '18 at 10:03
  • I did not notice its not `INCLUDEPATH += include/*` it should be `INCLUDEPATH += include/`. I believe that will fix your issue. Also when you set the `INCLUDEPATH` you'll not have to use `#include "../../file.h"`, you can just give `#include "file.h"` It will work. – Marcus Mar 14 '18 at 16:59
1

Qmake files that specify build directories as source subdirectories, and that assume that qmake is executed in the source folder, are essentially broken. The recommended way to build any project (even non-Qt projects!) is to do it out of source. I.e.:

# suppose the sources are in myproject
mkdir myproject-build
cd myproject-build
qmake ../myproject && make      # for qmake projects
cmake ../myproject && make      # for cmake projects
../myproject/configure && make  # for autotools projects

The sensible thing to do, of course, is to hardlink (or copy) all the executables into some common subfolder of the build directory. This requires some more gymnastics. E.g.:

EXTRA_BINFILES += $$PWD/src/myproject # target name in src.pro
DESTDIR = $$OUT_PWD/bin
QMAKE_PRE_LINK += $$QMAKE_MKDIR_CMD $$DIR
for(FILE, EXTRA_BINFILES){
  QMAKE_POST_LINK += \
    $$QMAKE_COPY $$shell_path($$FILE) $$shell_path($$DIR) $$escape_expand(\\n\\t)
}

See this question for more insight.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313