17

I have a file "settings.ini" which needs to reside next to the Qt executable.

I can add a custom build step for this in Qt Creator which calls something like this:

copy %{sourceDir}/settings.ini %{buildDir}/settings.ini

This works great so far, but I'd like to include this in the *.pro file so I can put this up in our SVN too.

How can I do this using qmake/.pro-files only?

Strayer
  • 3,050
  • 3
  • 30
  • 40

4 Answers4

18

To copy %{sourceDir}/settings.ini to the build directory without requiring to call make install use:

copydata.commands = $(COPY_DIR) $$PWD/settings.ini $$OUT_PWD
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata

$$PWD is the path of current .pro file. If your settings.ini file is not located in the same directory than the project file, then use something like $$PWD/more_dirs_here/settings.ini

Note: I found this solution here. I recommend to read the whole article as it explains how it works.

Paglian
  • 6,162
  • 3
  • 26
  • 20
  • 3
    This appears to have the rather nasty habit under Windows of ending up with the command `xcopy /s /q /y /i c:/source/file c:/dest/file` - `xcopy` does *not* like the forward slashes since it seems to treat them as switches leading to a "incorrect number of args" error. – paxdiablo Jul 28 '16 at 02:58
  • 2
    use `copydata.commands = $(COPY_DIR) $$shell_path($$PWD/settings.ini) $$shell_path($$OUT_PWD)` to avoid the slash problem under windows – Jiloc Feb 15 '17 at 17:35
  • How about multiple files? e.g: 1.ini and 2.ini – SuB Oct 12 '22 at 05:38
11

You probably want to use the INSTALLS keyword in QMake. It will require you to run make install after your build, but it does work cross-platform.

install_it.path = %{buildDir}
install_it.files += %{sourceDir}/settings.ini

INSTALLS += install_it
jwernerny
  • 6,978
  • 2
  • 31
  • 32
  • Yes, I've seen lots of these. But I guess this won't work in Qt Creator when compiling, for example. – Strayer Oct 20 '11 at 10:02
  • 1
    @Strayer Why won't it work? Just add another build step, `make install`. I have several working examples of it working in Qt Creator on my machine right now. – jwernerny Oct 20 '11 at 12:12
  • Hmm, well, in that case sorry for my comment ;) I'll try this the next time I'm working on the project! – Strayer Oct 20 '11 at 13:47
  • 2
    I'm trying to get it to work, but `make install` says me: `Nothing to be done for 'install'`. What could be wrong? – Dmitry Frank Sep 28 '13 at 11:40
  • @DmitryFrank Without seeing what your .pro file looks like, it is hard to say. My guess is that the file you are trying to install does not exist at the time you run qmake. This [Qt Centre question](http://www.qtcentre.org/threads/35817-qmake-INSTALLS-variable-not-working-as-expected) might help. – jwernerny Sep 30 '13 at 13:20
  • 1
    @jwernerny sorry I haven't specified it here, but I already asked my own question and got the answer that worked for me: http://stackoverflow.com/questions/19066593/copy-a-file-to-build-directory-after-compiling-project-with-qt . Thank you. Here, I just thought that `%{buildDir}` and `%{sourceDir}` are already defined by Qt. – Dmitry Frank Oct 01 '13 at 18:58
  • Is there a simple way to copy all the files in a directory instead of listing them all out one by one? – djsosofresh Sep 20 '18 at 20:39
  • 1
    @djsosofresh I have not done it with using `make install`, but it can be done as suggested by Paglian's answer (https://stackoverflow.com/a/36438618/447438). – jwernerny Sep 24 '18 at 16:06
  • I don't want the *just another build step, `make install`*. – zwcloud Aug 23 '19 at 05:35
  • This approach seems to be the QMake standard way, but this adds extra steps for someone who like to build the project. so, my project couldn't be built out of the box. Have you any further point about this? – S.M.Mousavi Jan 30 '23 at 11:15
2

for osx bundles you can handle it this way see Resource files in OS X bundle

add this to you project file:

APP_QML_FILES.files = path/to/file1.qml path/to/file2.qml
APP_QML_FILES.path = Contents/Resources
QMAKE_BUNDLE_DATA += APP_QML_FILES

this example copies the files to Contents/Resources

lumos0815
  • 3,908
  • 2
  • 25
  • 25
0

Compatible with Windows and Mac OSX Dev environments:

Change {AppName} to respective application name

# Define mac/windows specific target dirs
TARGETDIR = ''
macx {
  TARGETDIR      += $$OUT_PWD/{AppName}.app/Contents/MacOS/
}
else {
  TARGETDIR      += $$OUT_PWD
}

# Directories do not exist for the first build
# Without mkdata, build is successful after 5 tries. To avoid, use mkdata
mkdata.commands = $(MKDIR) $${TARGETDIR}
copydata.commands = $(COPY_FILE) $$PWD/settings.ini $${TARGETDIR}
first.depends = $(first) mkdata copydata
export(first.depends)
export(mkdata.commands)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first mkdata copydata

Happy to add Unix support if someone posts Unix solution in the comments.

kingspp
  • 311
  • 1
  • 3
  • 12