Important: Please read the Qt Code of Conduct -

Qmake takes sources from other directory than told to

  • Lets have the following file structure:

    |-- lib.pri
    |-- lib.cpp
    |-- lib.h
    |-- /app
    |---- app.cpp
    |-- /lib
    |---- lib.pri
    |---- lib.cpp
    |---- lib.h

    The content of files is following:


    SOURCES += app.cpp


    SOURCES += $$PWD/lib.cpp
    HEADERS += $$PWD/lib.h

    And files app.cpp, lib.cpp and lib.h are just some dummy app/ lib code.
    When I run qmake/make in prj dir everything is ok.
    But when I make release directory and run qmake/make as qtcreator does

    mkdir release
    cd release
    qmake ../

    in output appears:

    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../app -I. -I../../lib -I../../../../tools/Qt5.5.0/5.5/gcc_64/include -I../../../../tools/Qt5.5.0/5.5/gcc_64/include/QtGui -I../../../../tools/Qt5.5.0/5.5/gcc_64/include/QtCore -I. -I../../../../tools/Qt5.5.0/5.5/gcc_64/mkspecs/linux-g++ -o app.o ../../app/app.cpp
    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../app -I. -I../../lib -I../../../../tools/Qt5.5.0/5.5/gcc_64/include -I../../../../tools/Qt5.5.0/5.5/gcc_64/include/QtGui -I../../../../tools/Qt5.5.0/5.5/gcc_64/include/QtCore -I. -I../../../../tools/Qt5.5.0/5.5/gcc_64/mkspecs/linux-g++ -o lib.o ../../../lib/lib.cpp

    The problem is on the second line, where file ../../../lib/lib.cpp is user for compilation instead of ../../lib/lib.cpp.
    When I remove lib directory from the highest level, qmake generates correct Makefile.

    Is this qmakes behavior correct?
    Is it my mistake?
    Is there any way to avoid this?

    Thank you, Martin

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should rather avoid building inside your sources. It would be better to use a folder that's at the same level as your project dir.

    If you still want to do that, you should use $ $PWD (without the space but since the forum is eating double dollars...) rather than .. for your include statement so it will use the full path rather than relative.

  • Hello,

    thank you much for answer, but:

    1. building this way is the default QtCreator behavior now, isn't it?
    2. Good notice, I forgot to mention. When i use include($$PWD/../lib/lib.pri), the result is the same (bad).

  • Lifetime Qt Champion

    1. No Qt Creator use shadow builds by default. If puts the shadow build folders at the same level as your sources.
    2. I would avoid having the same name reused (i.e. two lib.pri/lib.h/lib.cpp)

Log in to reply