20

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?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • 1
    Nice question! But... potential errors due to adding arbitrary files (from a directory) can be more than those when a file name is mistyped. Adding a file name to the .pri file takes much less time than adding a source file so this time should be neglected – mmonem Sep 27 '10 at 19:10

2 Answers2

17

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.

chalup
  • 8,358
  • 3
  • 33
  • 38
  • 10
    How can you make this recursively add all source files in a given directory and all sub directories? This only adds the files in the root path. – Dan Watkins Nov 13 '14 at 03:01
  • 2
    Nor does it seem to work for any path except $$PWD, e.g. `../my_blah/*.h` doesn't work, it says "Failure to find..." – DBedrenko Jan 04 '17 at 14:05
12

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
Fred
  • 4,894
  • 1
  • 31
  • 48