我正在开发GTK3应用程序,但是使用QTCreator作为我的IDE,只是因为我碰巧喜欢它。它工作正常,我可以轻松关闭所有特定于QT的内容并链接GTK库。只有一个小问题。
GTK使用XML文件定义其资源。它带有一个程序“ glib-compile-resources”,该程序使用一个.gresource.xml文件并生成一个.c文件*,然后可以将其包含在您的项目中。问题是QTCreator不了解glib-compile-resources,因此我必须记得每次对它们进行任何更改时都要手动运行该程序。
我尝试使用自定义构建步骤,但是如果这样做,那么QT每次都会重新构建文件,即使它没有更改也是如此,这会减慢该过程。此外,如果C文件尚不存在,它将失败,并显示“没有规则使目标x.o所需的目标x.c停止。”。错误,因此无论如何我都必须手动运行该程序。
是否有任何方法告诉QTCreator在遇到.gresource.xml文件时运行glib-compile-resources并将生成的C文件包含到最终编译中?
*除了直接的C源文件之外,还有其他可用的选项,但是C源对我来说是最简单和最容易的。
答案 0 :(得分:1)
您可以在qmake文件中添加自定义目标(而不是在QtCreator项目配置中)。请参阅https://doc.qt.io/qt-5/qmake-advanced-usage.html#adding-custom-targets和/或https://doc.qt.io/qt-5/qmake-advanced-usage.html#adding-compilers上的qmake文档。
更新:这是一个简单的示例,显示了如何使用.pro文件中的自定义目标机制针对单个文件执行此操作:
glib_resources.target = glib-resources.c
glib_resources.depends = glib-resources.xml
glib_resources.commands = glib-compile-resources --target $$glib_resources.target --generate-source $$glib_resources.depends
QMAKE_EXTRA_TARGETS += glib_resources
PRE_TARGETDEPS += glib-resources.c ## set this target as a dependency for the actual build
QMAKE_CLEAN += glib-resources.c ## delete the file at make clean
答案 1 :(得分:0)
这是我如何解决自己的问题的方法:
我在QT文档中发现了如何add your own custom compiler到QT项目文件。所需的确切行是:
GLIB_RESOURCE_FILES += \
resources.gresource.xml
# Add more resource files here.
glib_resources.name = glibresources
glib_resources.input = GLIB_RESOURCE_FILES
glib_resources.output = ${QMAKE_FILE_IN_BASE}.c
glib_resources.depend_command = glib-compile-resources --generate-dependencies ${QMAKE_FILE_IN}
glib_resources.commands = glib-compile-resources --target ${QMAKE_FILE_OUT} --sourcedir ${QMAKE_FILE_IN_PATH} --generate-source ${QMAKE_FILE_IN}
glib_resources.variable_out = SOURCES
glib_resources.clean = ${QMAKE_FILE_OUT}
QMAKE_EXTRA_COMPILERS += glib_resources
(感谢@zgyarmati,他的帖子使我得到了正确的答案。)