admin管理员组

文章数量:1200965

In qmake, I want to copy a file from my source directory to the build directory. I noticed that Windows uses \ in paths, so I made sure to replace it accordingly, as shown in the code below:

win32:CONFIG(debug, debug | release)
{
    # target file
    TargetConfig = $${PWD}/config.ini
    TargetConfig = $$replace(TargetConfig, /, \\)
    # output directory
    OutputDir =  $${OUT_PWD}/$${DESTDIR}
    OutputDir = $$replace(OutputDir, /, \\)
    # copy
    # QMAKE_POST_LINK += $$QMAKE_COPY_DIR \"$$TargetConfig\" \"$$OutputDir\" > log.txt
    QMAKE_POST_LINK += cmd /C copy \"$$TargetConfig\" \"$$OutputDir\" > log.txt
}

However, I found that after the build, the file was not copied. When I checked the log.txt file, it seemed to only contain some information about the cmd startup, meaning that the copy command was not executed. I then tried executing the copy command in the cmd terminal, and it successfully copied the file, so I was puzzled as to why it didn't work in the build.

After researching, I found that I could use QMAKE_COPY_DIR, which generates the appropriate copy command for different platforms. After using this command, the file was successfully copied during the build, and I found that the build log used the cp command for the copy operation, as shown in the image below:

I noticed this issue In mingw,qmake copy_dir is always error, but the answer to this question did not resolve my doubts. I am using QT 6.8.1, with mingw32-make as the build tool, and the operating system is Windows. Therefore, my questions are:

  • Why can't the copy command copy the file during the build process?
  • Why is the cp command from Unix used instead of the copy command from Windows?

In qmake, I want to copy a file from my source directory to the build directory. I noticed that Windows uses \ in paths, so I made sure to replace it accordingly, as shown in the code below:

win32:CONFIG(debug, debug | release)
{
    # target file
    TargetConfig = $${PWD}/config.ini
    TargetConfig = $$replace(TargetConfig, /, \\)
    # output directory
    OutputDir =  $${OUT_PWD}/$${DESTDIR}
    OutputDir = $$replace(OutputDir, /, \\)
    # copy
    # QMAKE_POST_LINK += $$QMAKE_COPY_DIR \"$$TargetConfig\" \"$$OutputDir\" > log.txt
    QMAKE_POST_LINK += cmd /C copy \"$$TargetConfig\" \"$$OutputDir\" > log.txt
}

However, I found that after the build, the file was not copied. When I checked the log.txt file, it seemed to only contain some information about the cmd startup, meaning that the copy command was not executed. I then tried executing the copy command in the cmd terminal, and it successfully copied the file, so I was puzzled as to why it didn't work in the build.

After researching, I found that I could use QMAKE_COPY_DIR, which generates the appropriate copy command for different platforms. After using this command, the file was successfully copied during the build, and I found that the build log used the cp command for the copy operation, as shown in the image below:

I noticed this issue In mingw,qmake copy_dir is always error, but the answer to this question did not resolve my doubts. I am using QT 6.8.1, with mingw32-make as the build tool, and the operating system is Windows. Therefore, my questions are:

  • Why can't the copy command copy the file during the build process?
  • Why is the cp command from Unix used instead of the copy command from Windows?
Share Improve this question asked Jan 21 at 15:05 吃花椒的喵酱吃花椒的喵酱 314 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

On Windows qmake genereates makefiles with cp instead of copy if cp is available, meaning if cp is found in %path% environment variable, that makes sense when you compile on msys2 shell or cygwin shell for example, and leads to very confusing errors sometimes when it's not intentional.

You probably added something like C:\msys64\usr\bin or C:\Program Files\Git\usr\bin into path by mistake or intentionally.

You can open shell and run where cp to find out offending path.

I checked it on my machine, it works. But theres some caviats: make does not rebuild target unless one of sources has changed (no linking means no postlink mingw32-make[1]: Nothing to be done for 'first'.), so command is not executed in this case. Run rebuild, remove target binary or touch any source file to trigger target build.

You can locate actual command in Makefile.Debug by searching config.ini and make sure its correct.

本文标签: qtWhy can39t the copy command copy the file during the build processStack Overflow