# Copying data files to the build directory with qmake

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.

### Svenn-Arne Dragly

I'm a physicist and programmer, writing about the stuff I figure out as I go.

## 2 thoughts on “Copying data files to the build directory with qmake”

1. robert says:

Hi, the script ‘s great! I’m using it copying some mp3 files into the App directory.

In the Qt creator it runs fine(Play mp3 with QMediaPlayer).

But if the app execute directly from Finder(the platform is MacOSX 10.9), there’s no sound at all!

Since it runs directly from Finder, there seems no any debug message. Is there anything I missed here?

Any comment are appreciated. Thanks for your help!

1. Hello,

You should try to open a Terminal in the same directory and run the app from there. That should be equivalent to running it from Finder (I think) and allow you to see the output of the application. If not, there is an application in Mac OS X that allows you to see all system logs. I believe this might log your application, if I remember correctly.

A better option is to use a Qt resource file, which bundles all the mp3-files into the executable. This makes the executable fairly large, but is definitely the easiest way to pack resources with your application. Check out the Qt documentation for more information.

Alternatively, you may look into bundling your files into a .app on Mac, which is the recommended way to bundle files that are not part of your executable. There is some information on how to do this in the Qt documentation and in an earlier blog post of mine.