40

I have seen several suggestions, but nothing really worked for me as I want. I just need to copy a file to the desired destination directory.

Say, for example from this answer:

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

INSTALLS += install_it

Variables %{buildDir} and %{sourceDir} should be defined, to make this work. Ok, there's no problem with %{sourceDir}: it is just .. But how can I get %{buildDir}?

EDIT1

Say, I have a project my_project here:

/path/to/my_project

So, release build path is this: /path/to/my_project-build-Desktop-release,

debug build path is this: /path/to/my_project-build-Desktop-debug

I have files to be copied to destination directory here: /path/to/my_project/copy_to_install_dir

So, I want all files from /path/to/my_project/copy_to_install_dir to be copied to /path/to/my_project-build-Desktop-release when I do release build. And, the same way for debug build.

I can't find variable which contain full destination path, i.e. /path/to/my_project-build-Desktop-release for debug build.

Just in case: I use Windows, but anyway I'm looking for crossplatform solution.

EDIT2

Exact solution, for future readers:

install_it.path = $$OUT_PWD
install_it.files = copy_to_install_dir/*

INSTALLS += \
    install_it
0

6 Answers 6

48

The selected answer is correct but it requires to call make install, which in my opinion is annoying or error prone. Instead, to copy files to the build directory use:

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

Where required_files must be replaced with your correct path. $$PWD is the path of current .pro file, you might not require this.

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

5
  • Is it possible to make a condition that would copy files/folders inside the Mybinary.app/Contents/MacOS on Mac? Nov 29, 2016 at 9:34
  • 1
    @alexandernst I think what you could do is to define a variable for the output dir according your OS. See doc.qt.io/qt-4.8/…
    – Paglian
    Nov 30, 2016 at 13:03
  • the article 404 not found
    – kyb
    Dec 14, 2017 at 17:24
  • @Paglian, Thank you for useful answer.
    – kyb
    Dec 14, 2017 at 17:35
  • need quotes for path with spaces: $$PWD/required_files -> \"$$PWD/required_files\". And quotes for out directory
    – Glebka
    Jun 29, 2022 at 12:52
22

Having had the pleasure of wasting a few hours with this, i thought i'd share my findings on the matter. This in a modified variant of Paglian's method here. Since i'm using windows (without mingw) that method doesn't work. So here is the modified variant:

# using shell_path() to correct path depending on platform
# escaping quotes and backslashes for file paths
copydata.commands = $(COPY_FILE) \"$$shell_path($$PWD\\archive.png)\" \"$$shell_path($$OUT_PWD)\"
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata

Since this makes it cross platform you could of course also use this method in Linux, MacOS or what have you. Do note that i'm copying a single file, so instead of $(COPY_DIR) i'm using $(COPY_FILE). Adapt as needed.


If you want the file(s) copied to the exact path of where the binary ends up (since the binary will end up in a subfolder of $$OUT_PWD (debug or release, at least when building with Qt Creator with MSVC 14/cdb.exe/Code::Blocks Makefiles configuration) you need this:

# adapted from https://stackoverflow.com/a/2581068
CONFIG(debug, debug|release) {
    VARIANT = debug
} else {
    VARIANT = release
}

Beware that even though the binary ends up in a subfolder, QtCreator executes the binary from $$OUT_PWD, so it expects to find file resources in $$OUT_PWD, and NOT the debug subdir. That means you for example can't do QIcon("archive.png") and expect it to find it besides the executable.

This is of course easy to remedy by doing:

QDir exeDir(QCoreApplication::applicationDirPath());
QIcon qIcon(exeDir.filePath("archive.png"));

IF you decide this is what you want, you obviously need to edit the last argument of $$(COPY_FILE) (in .pro) like this: \"$$shell_path($$OUT_PWD)\\$$VARIANT\"


One other thing to note is that (in my case anyway) Qt Creator (4.0.1) doesn't always build the .pro file, since it's not detecting any changes in the configuration, so to have the above changes reflected in the Makefile (and thus run when you build your project) you have to actually build the .pro manually by running Build->run qmake from the application menu. To ensure everything goes smooth, look as the compile output by hitting Alt+4 (on Windows anyway).

3
  • 1
    where did you find out about COPY_FILE or COPY_DIR from? In qt creator help mode, the qmake documentation had no hit for the word "copy" (other than copyright...) for me. I found stackoverflow.com/questions/20324061 but only after I read in your post that these exist.
    – n611x007
    Jan 24, 2017 at 14:19
  • To be honest, i just found them when tracking down what was happening behind the scenes. Can't remember the specifics of it, but i did manage to find this after the fact: wiki.qt.io/Undocumented_QMake
    – PKSWE
    Jan 30, 2017 at 20:54
  • 1
    How to copy all files in a subdir? I tried with $$PWD\\subdir\\* in the first row, but it gives me error. May 26, 2017 at 15:24
15

This is what we are using in QtSerialPort:

target_headers.files  = $$PUBLIC_HEADERS
target_headers.path   = $$[QT_INSTALL_HEADERS]/QtSerialPort
INSTALLS              += target_headers

mkspecs_features.files    = $$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf
mkspecs_features.path     = $$[QT_INSTALL_DATA]/mkspecs/features
INSTALLS                  += mkspecs_features

Basically, you set the files and path for the target, and then append that into the INSTALLS variable. What you still need is the $$OUT_PWD variable which we are also using extensively in QtSerialPort. That will provide you the root of the build directory.

It is one of those undocumented qmake features, but it is very useful.

Also, for the source directory in general at large, you should not assume "." and so forth because that may be different as you run through a wrapper application in which the "." will point to that and not what you expect: the qmake source project root. In those cases, it is safer to use the PWD variable which points to the source as opposed OUT_PWD which points to the build folder.

Just to give a rough example about the difference o those two variables with a real world scenario, here you can find what we are doing in QtSerialPort:

system("echo QTSERIALPORT_PROJECT_ROOT = $$PWD >> $$OUT_PWD/.qmake.cache")
system("echo QTSERIALPORT_BUILD_ROOT = $$OUT_PWD >> $$OUT_PWD/.qmake.cache")

where the former is the root for the source project, and the latter for the build directory. They may be the same, but in many cases they are not, e.g. when building through QtCreator just for one of those.

2
  • 5
    This does the thing only with make install, see stackoverflow.com/a/36438513/3129752 for doing it at make. Apr 14, 2017 at 13:08
  • Thanks! Finally managed to make it work (I had configured make install already). Just a note: system("echo ..") doesn't work for me, I used message() instead of.
    – Jack
    Feb 5, 2018 at 18:11
4

Maybe the following QMake code helps as a starting point. It copies the recently built binary to some other directory "TARGET_DEST":

TARGET_SRC  = $${_PRO_FILE_PWD_}
TARGET_DEST = $${PWD}/src

CONFIG(debug, debug|release) {
    TARGET_SRC = $${TARGET_SRC}/debug
} else {
    TARGET_SRC = $${TARGET_SRC}/release
}

TARGET_SRC   = $${TARGET_SRC}/$${TARGET}
TARGET_DEST  = $${TARGET_DEST}/$${TARGET}

linux-g++{
    if( equals(TEMPLATE, app) || equals(TEMPLATE, vcapp) ){
        # nothing to do here
    }
    if( equals(TEMPLATE, lib) || equals(TEMPLATE, vclib) ){
        TARGET_SRC   = $${TARGET_SRC}.so
        TARGET_DEST  = $${TARGET_DEST}.so
    }
    QMAKE_POST_LINK += $$quote(cp $${TARGET_SRC} $${TARGET_DEST}$$escape_expand(\n\t))
}

win32 {
    if( equals(TEMPLATE, app) || equals(TEMPLATE, vcapp) ){
        TARGET_SRC   = $${TARGET_SRC}.exe
        TARGET_DEST  = $${TARGET_DEST}.exe
    }
    if( equals(TEMPLATE, lib) || equals(TEMPLATE, vclib) ){
        TARGET_SRC   = $${TARGET_SRC}.dll
        TARGET_DEST  = $${TARGET_DEST}.dll
    }
    TARGET_SRC  ~= s,/,\\,g # fix slashes 
    TARGET_DEST ~= s,/,\\,g # fix slashes
    QMAKE_POST_LINK +=$$quote(cmd /c copy /y $${TARGET_SRC} $${TARGET_DEST}$$escape_expand(\n\t))
}

message("[INFO] Will copy $${TARGET_SRC} to $${TARGET_DEST}")
1
  • 1
    Upvoted for using QMAKE_POST_LINK rather than INSTALLS or QMAKE_EXTRA_TARGETS.
    – BuvinJ
    Dec 20, 2017 at 0:41
3

You can use DESTDIR and PWD qmake variables or OUT_PWD: http://qt-project.org/doc/qt-5.1/qmake/qmake-variable-reference.html#destdir

1
  • 4
    When I'm not sure for qmake variables, I use message : message($$OUT_PWD). It's usefull
    – gbdivers
    Sep 28, 2013 at 12:30
0

This in a modified variant of PKSWE's method.

dummyTarget.commands = @echo After building copy..
QMAKE_EXTRA_TARGETS += dummyTarget
PRE_TARGETDEPS += dummyTarget

toolsCopy.commands = $(COPY_DIR) $$shell_path($$PWD/copyDir/*) $$shell_path($$DESTDIR)
dummyTarget.depends += toolsCopy
QMAKE_EXTRA_TARGETS += toolsCopy

toolsCopyLib.commands = $(COPY_FILE) $$shell_path($$PWD/setting.ini) $$shell_path($${DESTDIR})
dummyTarget.depends += toolsCopyLib
QMAKE_EXTRA_TARGETS += toolsCopyLib

But, I have an other question, how to copy if changed? Since it takes too much time when it doesn't need to copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.