Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMake: Automatically compiling all files in a directory

Tags:

qt

qmake

For my Qt project, I use a .pro file that includes a separate .pri file for the various header, source, form and resource files. However, every time I add a new file I need to manually add it to the .pri file. This is tedious and error-prone. Is there a way to "magically" add all files from a directory, either directly in the .pri file or by telling qmake to run a separate script beforehand?

like image 367
Etienne de Martel Avatar asked Sep 27 '10 18:09

Etienne de Martel


People also ask

What is the difference between qmake and CMake?

Both are build systems, but they're not very similar at all. If your project uses Qt, you're probably best off using qmake. CMake is more generic, and fits pretty much any type of project. Both qmake and CMake generate a Makefile, which is read by make to build the project.

What is qmake file?

QMake is a build system that generates Makefiles for GNU Make or project build files for Microsoft Visual Studio. It is part of the Qt software framework by Trolltech. While it is commonly used to construct Qt-based software, any project can benefit from it.

How do you use qmake?

For simple projects, you only need to run qmake in the top level directory of your project. By default, qmake generates a Makefile that you then use to build the project, and you can then run your platform's make tool to build the project. qmake can also be used to generate project files.

What is .pro file in Qt?

A .pro file is actually the project file used by qmake to build your application, library, or plugin. It contains all the information, such as links to the headers and source files, libraries required by the project, custom-build processes for different platforms/environments, and so on.


2 Answers

You can use:

SOURCES += *.cpp
HEADERS += *.h

in your pro file. Of course you still have to remember to re-run qmake after creating new files.

like image 146
chalup Avatar answered Sep 24 '22 06:09

chalup


Running qmake -project from the directory will create a project file that includes all the .cpp and .h files in that directory. You could add a pre-compile step that calls qmake -project, then pass the generated file to a script that removes the first few lines. Here's a quick one-liner that could do the job :

qmake -project -o MyFiles.pro && sed '1,/^# Input/d' MyFiles.pro > MyFiles.pri && rm MyFiles.pro
like image 41
Fred Avatar answered Sep 22 '22 06:09

Fred