Sometimes you want to copy some data files to the same folder as your final executable when you are building with qmake in a different directory than your source code. This is what Qt Creator does by default, so this is quite often needed.
Just add the following to your .pro file to copy the folder “data” from the source directory to the build directory whenever you build the source code:
copydata.commands = $(COPY_DIR) $$PWD/data $$OUT_PWD first.depends = $(first) copydata export(first.depends) export(copydata.commands) QMAKE_EXTRA_TARGETS += first copydata
The meaning of the different names in the above are as follows:
- $(COPY_DIR) – Holds a platform-dependent copy-command that makes sure to copy recursively.
- $$PWD – Holds the name of the source code directory, where your .pro file resides.
- $$OUT_PWD – Holds the name of the build directory. Note that this may not work if you are running qmake and make in the same directory as your source code, as you will be copying into the same folder as you copy from.
- copydata – Just a name we choose. You can replace this with whatever you want.
- first – This is a build step that already exists in the Makefile generated by qmake. We attach the copy build step to this build step.
- export – Makes the variable in the argument of the function available in a global scope.
- QMAKE_EXTRA_TARGETS – Tells qmake that you want to build more targets than the ones it adds by default to the Makefile.
This method is a bit messy, and I wish the Qt developers would make it easier to do this, but it works. It also ensures that the data is always copied, ensuring that any changes in the data folder are pushed to the build folder.