Why does qmake put all object (.o) files to one directory?
Asked Answered
J

3

24

Let's say I have a Qt application where I have two classes with the same name in two different namespaces:

namespace namespace1
{
    class SomeClass;
}

namespace namespace2
{
    class SomeClass;
}

and I have a project directory structure according to it:

-->src/
  -->namespace1/
    -->someclass.cpp
  -->namespace2/
    -->someclass.cpp

When I compile the application with qmake, it puts all object (.o) files to one directory - so it creates someclass.o file first and then it rewrites it with the second someclass.o - which is a name collision so it is bad.

Why does qmake not take into account the directory structure of the source files and why does it not create something like namespace1_someclass.o and namespace2_someclass.o?

Yes, I can put my classes to one directory and name them namespace1_someclass.cpp and namespace2_someclass.cpp and there will be no name collisions, but this causes little inconvenience while looking at the source files in the project explorer in Qt Creator because when there are lot of source files in the project, it is much less readable than if there was the directory structure which I can expand or collapse.

One more extreme is to have the directory structure like this:

-->src/
  -->namespace1/
    -->namespace1_someclass.cpp
  -->namespace2/
    -->namespace2_someclass.cpp

which solves name collision but it redundantly duplicates the namespace names - and therefore again less readable.

Why does qmake not have at least an option to put the object files to the directory structure according to the source files? Do creators of Qt not see that this is an important feature?

And one more thing - you could recommend me to use cmake tool instead of qmake but I see the use of cmake much much much more difficult than qmake and qmake does its job excellent for me so far - except object files placement.

Johnsen answered 26/2, 2012 at 3:18 Comment(1)
I can only guess about what the authors of qmake were thinking; my guess is that they just didn't anticipate that anyone would have multiple .cpp files with the same name in the same project. FWIW they are working on a replacement for qmake called qbs ... perhaps it does a better job (I haven't tried it myself so I don't know): labs.qt.nokia.com/2012/02/15/introducing-qbsLanguage
E
16

You can actually put object files alongside source files by using:

CONFIG += object_parallel_to_source

or

CONFIG += object_with_source

depending on your qmake version.

Source: https://wiki.qt.io/Undocumented_QMake#Config_features

Earhart answered 12/11, 2015 at 16:44 Comment(3)
Not sure why this does not have more upvotes. CONFIG += object_parallel_to_source on Qt5 worked perfectly for me.Sporocyst
Shouldn't this be on by default? Can anybody think of a possible problem or performance penalty of using this every time?Custommade
It works if the class does not inherits from QObject. If the class inherits from QObject, Qt still generates all the moc_*.cpp files in the same place... Any idea?Andrey
F
3

Depending on what you are trying to build, you may be able to use the subdirs template in qmake to do this. You'll need to put a project file in each of your namespace directories, and in this you can specify different output directories for your object files.

-->src/main.pro
  -->namespace1/n1.pro
    -->someclass.cpp
  -->namespace2/n2.pro
    -->someclass.cpp

main.pro:

TEMPLATE = subdirs
SUBDIRS  = namespace1 namespace2

n1.pro and n2.pro:

include("../common.pri")
OBJECTS_DIR = $${PWD}
TARGET = some_target
TEMPLATE = some_qmake_template

common.pri: configurations common to both projects.

Farrago answered 13/8, 2012 at 15:30 Comment(0)
O
2

Concerning your fears that CMake might be too complicated: I have been working on projects using both build systems. While I agree that qmake is probably easier to begin with, CMake definitely has its merits, too:

  • It makes out-of-source builds very easy. Just execute cmake <Path to source> in your build directory. This is great when your sources are on an NFS share, for example, and you want the object files to be placed on a local file system.

  • Its support for finding additional libraries is very powerful. Lots of FindXXX.cmake files are already shipped with your CMake distribution, making the inclusion of "heavy" libraries such as OpenCV as easy as FIND_PACKAGE(OpenCV REQUIRED).

  • It even has out-of-the-box support for Qt. In fact, I use it for a larger software project where Qt is used for the GUI part. We decided on CMake because we required platform independence and multiple libraries which we could not easily add via qmake.

All in all, use the build system you are comfortable with (as long as your build system does not inhibit your software development).

Odontograph answered 20/5, 2012 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.