如何在CMake中使用glib-compile-resources [英] How to use glib-compile-resources with CMake

查看:210
本文介绍了如何在CMake中使用glib-compile-resources的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着任何GTK项目的发展,GTK应用程序都倾向于与gresources捆绑在一起以分离出代码和UI设计.这非常有用,因为UI/UX设计师不需要知道代码就可以...进行良好的设计并最终为该项目贡献自己的技能和精力.

As any GTK project grows, GTK applications tend to be bundled with gresources to separate out code and UI design. This is very useful because UI/UX designers don't need to know code in order to... well design and ultimately contribute their skills and effort to the project.

不仅设计师,而且程序员也受益匪浅!因为代码变得非常逻辑或问题解决",而不是将UI和逻辑代码同时保存在一个文件中.

Not only designers but programmers too benefit a lot! Because code becomes heavily "logic or problem solving" instead of maintaining both UI and logic code together in one single file.

但是,要编译我们的GResource,我们需要 glib-compile-resources 实用工具.该命令通常如下所示:

However, to compile our GResource we need glib-compile-resources utility tool. The command usually goes like this:

glib-compile-resources --generate-source --target=<output-file> <input-file>

但是我如何创建一个编译脚本来编译我们的gresource文件并将其链接到我们的目标项目呢?我仍然是学习CMake的新手,我已经足够了解到目标是什么,如何设置变量,如何链接目标以及如何获取所需的GTK软件包以进行链接.但是我不知道如何继续解决这个问题:(

But how do I create a build script that compiles our gresource files and link it with our target project? I'm still a newbie learning CMake and I've gotten far enough to know what a target is, how to set a variable, how to link a target, and also how to pull in the required GTK packages for linking. But I don't have any clue how to proceed ahead with solving this :(

推荐答案

一个解决方案是使用 add_custom_command()编译您的gresources.但是首先,这是您对CMake脚本的需求的细分:

A solution to this is using add_custom_command() to compile your gresources. But first here's a breakdown of what you need for your CMake script:

  1. glib-compile-resources 作为可执行程序- find_program()
  2. 定义如何编译您的gresource- add_custom_command()
  3. 然后定义您的自定义目标- add_custom_target()
  4. 告诉我该资源是一个生成的文件- set_source_files_properties()
  5. 最后,将自定义目标作为依赖项添加到项目目标中- add_dependencies()
  1. Pull in glib-compile-resources as executable program - find_program()
  2. Define how to compile your gresource - add_custom_command()
  3. Then define your custom target - add_custom_target()
  4. Tell CMake that resource is a generated file - set_source_files_properties()
  5. Finally, add your custom target to your project target as a dependency - add_dependencies()

这是一个示例CMake脚本:

Here's a sample CMake script:

cmake_minimum_required(VERSION 3.15)

project(dummy)

# Step 1:
find_program(GLIB_COMPILE_RESOURCES NAMES glib-compile-resources REQUIRED)

set(GRESOURCE_C   test.gresource.c)
set(GRESOURCE_XML test.gresource.xml)

# Step 2:
add_custom_command(
    OUTPUT ${GRESOURCE_C}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMAND ${GLIB_COMPILE_RESOURCES}
    ARGS
        --target=${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
        ${GRESOURCE_XML}
    VERBATIM
    MAIN_DEPENDENCY ${GRESOURCE_XML}
    DEPENDS
        for.glade
        bar.glade
)

# Step 3:
add_custom_target(
    dummy-resource
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
)

# Step 4:
add_executable(${PROJECT_NAME} dummy.c ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
set_source_files_properties(
    ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
    PROPERTIES GENERATED TRUE
)

# Step 5:
add_dependencies(${PROJECT_NAME} dummy-resource)

简要说明

add_custom_command()

  • 输出-这是您生成的资源文件
  • WORKING_DIRECTORY -XML和Glade文件所在的位置
  • VERBATIM -确保我们的 COMMAND 收到的 ARGS 不变
  • MAIN_DEPENDENCY -用于glib-compile-resources< input-file>
  • DEPENDS -您的林间空地文件.如果文件有任何更改,则触发您的目标构建:)
  • Brief explanation

    add_custom_command()

    • OUTPUT - This is your generated resource file
    • WORKING_DIRECTORY - Where your XML and glade files are located
    • VERBATIM - Makes sure our COMMAND receives ARGS unchanged
    • MAIN_DEPENDENCY - for glib-compile-resources <input-file>
    • DEPENDS - Your glade file(s). If any of the file changes then your target build is triggered :)
      • 虚拟资源-这是您的自定义目标名称
      • DEPENDS -自定义目标触发触发自定义命令所需的输出
      • dummy-resource - That's your custom target name
      • DEPENDS - The output your custom target needs in order to trigger your custom command

      首次使用 cmake 命令生成构建文件时,尚未生成资源文件.因此CMake会出错,因为它不知道您的资源文件在哪里或从哪里来.我们需要告诉CMake不要失败,我们的资源文件会在以后生成".

      When you first generate your build files using cmake command, your resource file isn't generated yet. So CMake will run into error because it doesn't know where your resource file is or where it's coming from. We need to tell CMake "Don't fail, our resource file is generated later"

      现在您可能会注意到我们正在重复工作,即,当我们添加新的空地文件或删除现有的空地文件(或其他任何资源,例如图标,声音,css文件等)时,我们必须同时编辑XML和CMake脚本文件. glib-compile-resources 已经提供了依赖项生成,因此我们可以在CMake脚本中使用它并使其变得聪明.

      Now you might notice we are duplicating our effort ie., when we add new glade files or remove existing ones (or any other resources such as icon, sounds, css files, etc) we have to edit both our XML and CMake script files. glib-compile-resources already provide dependency generation so we can use that in our CMake script and make it smart.

      诀窍是将您的.xml文件更改为.xml.in作为配置文件.因此,当该配置文件更改时,您可以使用--generate-dependencies调用glib工具,获取新的依赖项输出值,并将其发送到 add_custom_command(... DEPENDS).现在我们有了一个智能的CMake:)

      The trick is to change your .xml file to .xml.in as a configuration file. So when that configuration file changes, you call glib tool with --generate-dependencies, get new dependency output values, and send that to add_custom_command(... DEPENDS). Now we have an intelligent CMake :)

      如果您想采用这种方法,那么下面的帖子将非常有帮助:

      If you want to approach this method then the below post would be really helpful:

      使用列表作为对add_custom_command的依赖

      祝你好运:)

      这篇关于如何在CMake中使用glib-compile-resources的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆