使用Qt编译项目后将文件复制到build目录

发布于 2024-12-10 09:01:53 字数 276 浏览 0 评论 0原文

我有一个文件“settings.ini”,需要驻留在 Qt 可执行文件旁边。

我可以在 Qt Creator 中为此添加一个自定义构建步骤,它调用如下内容:

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

到目前为止,效果很好,但我想将其包含在 *.pro 文件中,这样我也可以将其放入我们的 SVN 中。

我怎样才能仅使用 qmake/.pro-files 来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

温柔戏命师 2024-12-17 09:01:53

要将 %{sourceDir}/settings.ini 复制到构建目录而不需要调用make install,请使用:

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< /code> 是当前.pro 文件的路径。如果您的 settings.ini 文件与项目文件不在同一目录中,请使用类似 $$PWD/more_dirs_here/settings.ini

注意< /strong>:我找到了这个解决方案此处。我建议阅读整篇文章,因为它解释了它是如何工作的。

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.

半暖夏伤 2024-12-17 09:01:53

您可能想要使用 INSTALLS 关键字 在 QMake 中。它将要求您在构建后运行 make install,但它确实可以跨平台工作。

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

INSTALLS += install_it

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
亚希 2024-12-17 09:01:53

对于 osx 捆绑包,你可以这样处理
请参阅 OS X 捆绑包中的资源文件

将其添加到您的项目文件中:

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

此示例将文件复制到 Contents/资源

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

酒几许 2024-12-17 09:01:53

与 Windows 和 Mac OSX 开发环境兼容:

将 {AppName} 更改为相应的应用程序名称

# 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

如果有人在评论中发布 Unix 解决方案,我很高兴添加 Unix 支持。

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.

马蹄踏│碎落叶 2024-12-17 09:01:53

对于任何想要复制多个文件或目录的人:扩展 Paglian 的答案,我发现使用命令添加另一个变量是有效的。

copyconfig.commands = $(COPY_DIR) $shell_path($PWD\config) $shell_path($DESTDIR\config)
copystyle.commands = $(COPY_DIR) $shell_path($PWD\style) $shell_path($DESTDIR\style)
first.depends = $(first) copyconfig copystyle
export(first.depends)
export(copyconfig.commands)
export(copystyle.commands)
QMAKE_EXTRA_TARGETS += first copyconfig copystyle

For anyone who is looking to copy multiple files or directories: Expanding on Paglian's answer, I found that adding another variable with commands worked.

copyconfig.commands = $(COPY_DIR) $shell_path($PWD\config) $shell_path($DESTDIR\config)
copystyle.commands = $(COPY_DIR) $shell_path($PWD\style) $shell_path($DESTDIR\style)
first.depends = $(first) copyconfig copystyle
export(first.depends)
export(copyconfig.commands)
export(copystyle.commands)
QMAKE_EXTRA_TARGETS += first copyconfig copystyle
黯淡〆 2024-12-17 09:01:53

我最终创建了一个 python 脚本,在其中复制文件。

然后在 .pro 文件中添加以下行:

QMAKE_POST_LINK += python $PWD/copy_requirements.py $PWD $OUT_PWD

我使用 $$PWD 传递源目录,并使用 $$OUT_PWD 传递构建目录。

然后,python copy_requirements.py 脚本使用这些变量将所需的文件从源目录复制到构建目录。这是代码编译后执行的。

这适用于 Windows / Linux。根据 Qt 文档,这不适用于 Xcode 项目。

I ended up creating a python script where I do the copy of the files.

Then in the .pro file I add the following line:

QMAKE_POST_LINK += python $PWD/copy_requirements.py $PWD $OUT_PWD

I pass the source directory with $$PWD and the build directory with $$OUT_PWD.

The python copy_requirements.py script then uses these variables to copy the files needed from the source directory to the build directory. This is executed after the code compilation.

This works on windows / linux. According to Qt documentation this doesn't work on Xcode projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文