How to put generated files (e.g. object files) into a separate folder when using Qt/qmake?

一个人想着一个人 提交于 2019-12-18 12:17:20

问题


I have a Qt project that uses qmake. To improve clarity and readability, I'd like to keep the

  • source files
  • build system
  • generated files (such as object files)

separate.

So my first step was putting the source files into a src/ sub directory:

myproject/
    myproject.pro
    src/
        main.cpp
        MainWindow.ui
        ...

That way I separated the source files from the build system (*.pro). However, when I then run qmake followed by make, the generated files (object files, etc) are placed into the main project folder:

myproject/
    myproject.pro
    Makefile
    main.o
    ui_MainWindow.h
    ...
    src/
        main.cpp
        MainWindow.ui
        ...

Well, at least they weren't put into the src/ folder, but how do I specify that they are put into another sub folder such as build/?

myproject/
    myproject.pro
    Makefile
    build/
        main.o
        ui_MainWindow.h
        ...
    src/
        main.cpp
        MainWindow.ui
        ...

(BTW, I don't care where the target binary myproject is put, but I guess it should be placed directly into project folder rather than into build/.)


回答1:


You can add the following lines to your *.pro file:

DESTDIR=bin #Target file directory
OBJECTS_DIR=generated_files #Intermediate object files directory
MOC_DIR=generated_files #Intermediate moc files directory

A list of variables is available at the following locations:

  • Qt 4.8
  • Qt 5



回答2:


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

object_with_source — outputs each object file into the same directory as its source file (replaced by object_parallel_to_source in the latest versions).
object_parallel_to_source — recreate source folder tree for object files (replaces object_with_source).

in *.pro write

CONFIG += object_parallel_to_source


来源:https://stackoverflow.com/questions/3440387/how-to-put-generated-files-e-g-object-files-into-a-separate-folder-when-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!