qtqmake

QMake: Automatically compiling all files in a directory


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?


Solution

  • Running qmake -project will create a project file that includes all the .cpp and .h files in a directory, including subdirectories. You could add a pre-compile step that calls qmake -project, and only keep the part where the input files are defined. Here's a quick one-liner:

    Linux:

    qmake -project -o - | sed '1,/^# Input/d' > file_list.pri
    

    Windows: (PowerShell command - only works with ASCII file and folder names due to various text encoding issues)

    (qmake -project -o -) -ne "" -join "`r`n" -replace "(?s)^.*`r`n# Input`r`n", "" | Out-File -encoding ascii file_list.pri
    

    Don't forget to include(file_list.pri) within your .pro file.