QT项目:子文件夹复制到输出目录失败,make提示无copyfiles规则
Hey there! Let's fix that cross-platform folder copying issue in your Qt project without relying on OS-specific build steps. The "No rule to make target 'copyfiles'" error usually pops up because the build system doesn't recognize the custom target you're referencing—let's replace that with proper, Qt-native cross-platform configurations.
If you're using .pro files with qmake, you can leverage qmake's built-in functions to handle file copying across Windows, macOS, and Linux without platform-specific hacks:
# Define your source and target folders (use $$PWD for project root relative paths) SOURCE_SUBFOLDER = $$PWD/your_subfolder_name TARGET_SUBFOLDER = $$OUT_PWD/your_subfolder_name # Traverse all files in the source folder (including subdirectories) FILES_TO_COPY = $$files($$SOURCE_SUBFOLDER/*, true) # Map each source file to its corresponding target path for(file, FILES_TO_COPY) { # Get the path relative to the source folder to preserve structure RELATIVE_PATH = $$relative_path(file, SOURCE_SUBFOLDER) # Build the full target file path TARGET_FILE = $$TARGET_SUBFOLDER/$$RELATIVE_PATH # Add to qmake's copy list (handles cross-platform pathing automatically) COPY_FILES += $$file $$TARGET_FILE }
How this works:
$$files()recursively grabs all files in your source subfolder.$$relative_path()preserves the folder structure so your copied files match the original hierarchy.COPY_FILESis a qmake variable that tells the build system to copy these files to the target directory during the build process—no customcopyfilestarget needed, which eliminates that "no rule" error.
If you need to copy empty folders (since $$files() won't pick them up), add a pre-build step to create the target folder:
# Create target folder before building (cross-platform) win32 { QMAKE_PRE_LINK += if not exist \"$$TARGET_SUBFOLDER\" mkdir \"$$TARGET_SUBFOLDER\" && } else { QMAKE_PRE_LINK += mkdir -p $$TARGET_SUBFOLDER && }
If you're using CMake (the more modern choice for Qt projects now), CMake has a built-in cross-platform command for copying directories:
# Define source and target folders set(SOURCE_SUBFOLDER "${CMAKE_CURRENT_SOURCE_DIR}/your_subfolder_name") set(TARGET_SUBFOLDER "${CMAKE_CURRENT_BINARY_DIR}/your_subfolder_name") # Add a post-build command to copy the directory add_custom_command( TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${SOURCE_SUBFOLDER} ${TARGET_SUBFOLDER} COMMENT "Copying subfolder to output directory..." )
How this works:
add_custom_commandruns after your target is built.${CMAKE_COMMAND} -E copy_directoryis a cross-platform utility—CMake automatically uses the correct copy command (likexcopyon Windows orcp -ron Unix systems) so you don't have to write OS-specific code.- This will also recreate the folder structure and update files whenever the source content changes.
Why you got the "No rule to make target 'copyfiles'" error:
Chances are you referenced a custom copyfiles target in your build configuration but never defined it in your .pro or CMakeLists.txt. The solutions above avoid custom targets entirely by integrating directly with Qt's build system, so the error should disappear.
Hope these cross-platform fixes get your folder copying working smoothly! Let me know if you need further tweaks.
内容的提问来源于stack exchange,提问作者Pyromanci

