如何在CMake中使用qt5_add_binary_resources生成rcc文件

pb3skfrl  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(166)

我正在尝试使用CMake创建rcc文件,使用qt5_add_binary_resources(target inputfile ... OPTIONS ... DESTINATION ...)macro

qt5_add_binary_resources(myApp "themes/redTheme.qrc" OPTIONS ARGS -binary)

字符串
由于我在CMakeLists文件中使用了其他宏,如add_executable(要求使用target),因此出现以下错误:

CMake Error at C:/Qt/5.5/msvc2013/lib/cmake/Qt5Core/Qt5CoreMacros.cmake:255 (add_custom_target):
  add_custom_target cannot create target
  "myApp" because another target with the
  same name already exists.  The existing target is an executable created in
  source directory [..]


请注意,我的CMakeLists文件嵌套在根CMakeLists中。

**编辑:**我看了一下qt5_add_binary_resources宏的definition(l.226),最后一行是引起我错误的命令,它似乎没有做任何事情:

add_custom_target(${target} ALL DEPENDS ${rcc_destination})
我不明白为什么宏需要一个target

**编辑:**这里是我的CMakeLists.txt的内容,尽管为了清晰起见进行了简化。

CMAKE_MINIMUM_REQUIRED(VERSION 3.4)
CMAKE_POLICY(SET CMP0002 NEW)

PROJECT(myApp)

SET(RCC_EXECUTABLE ${CMAKE_PREFIX_PATH}/bin/rcc.exe)

# Find includes in corresponding build directories
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
SET(CMAKE_AUTOMOC ON)
SET(CMAKE_AUTORCC ON)

FIND_PACKAGE(Qt5Core 5.5.0 REQUIRED)
FIND_PACKAGE(Qt5Widgets 5.5.0 REQUIRED)
FIND_PACKAGE(Qt5Qml 5.5.0 REQUIRED)
FIND_PACKAGE(Qt5Quick 5.5.0 REQUIRED)

INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIRS})
# etc.

SET(myApp_LIBRARIES
    ${BOOST_THREAD_LIBRARIES}
    ${BOOSTALL_LIBRARIES}
    Qt5::Core
    Qt5::Widgets
    Qt5::Qml
    Qt5::Quick
)

SET(myApp_LIBRARIES
        ${myApp_LIBRARIES}
)

SET(myApp_SOURCES
    source/main.cpp
)

SET(myApp_RESOURCES
    resources/qml.qrc
)

SET(myApp_EXTERNAL_BINARY_RESOURCES
    themes/redTheme.qrc
    themes/blueTheme.qrc
)

ADD_EXECUTABLE(myApp WIN32
        ${myApp_SOURCES} ${myApp_RESOURCES}
)

FOREACH(_qrc_file ${myApp_EXTERNAL_BINARY_RESOURCES})
    STRING(REPLACE "qrc" "rcc" _rcc_file ${_qrc_file})
    # This is where I'd like to create my binary resources
    # QT5_ADD_BINARY_RESOURCES("test.rcc" ${_qrc_file})
    # Current working process, but not clean
    EXECUTE_PROCESS(COMMAND ${RCC_EXECUTABLE} -binary ${CMAKE_CURRENT_SOURCE_DIR}/${_qrc_file} -o ${CMAKE_CURRENT_SOURCE_DIR}/${_rcc_file})
ENDFOREACH()


根据这个文件,我试图生成两个rcc文件:redTheme.rccblueTheme.rcc

suzh9iv8

suzh9iv81#

顺便说一下.你不需要传递“OPTIONS ARGS -binary”,因为它已经定义为二进制。你需要传递一个目标名称,因为你可以调用“make targetname”手动生成rcc文件。此外,rcc文件将由“all”目标生成,如果你使用add_dependeny。rcc文件不会在cmake configure时生成,而是在编译时生成。如果文件发生更改,它也会重新生成。
因此,您需要按照错误提示传递一个未使用的目标名称!

c0vxltue

c0vxltue2#

如果您希望在修改.rcc文件的组成文件时重新构建.rcc文件,则必须生成依赖项列表。此外,使用execute_process意味着命令在运行cmake时执行,而不是在构建应用程序时执行,这可能不是您想要的。

# Get rcc command path and put it in RCC
get_target_property(RCC
    Qt5::rcc
    IMPORTED_LOCATION
)

# For each font file...
foreach(fnt IN LISTS FontResFile)

    # Generate dependencies
    execute_process (
        COMMAND "${RCC}" --list ${CMAKE_CURRENT_SOURCE_DIR}/${fnt}.qrc
        OUTPUT_VARIABLE deps
    )

    # convert to a list and put in deps2
    string(REGEX REPLACE "\n" ";" deps2 "${deps}")

    # Add command to build font rcc file
    add_custom_command (
        OUTPUT ${UI_OUTPUT_DIR}/${fnt}.rcc
        COMMAND Qt5::rcc --binary ${CMAKE_CURRENT_SOURCE_DIR}/${fnt}.qrc --compress 9 --threshold 10 -o ${UI_OUTPUT_DIR}/${fnt}.rcc
        MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${fnt}.qrc
        DEPENDS ${deps2}
        VERBATIM
    )
    add_custom_target (${fnt}_tgt DEPENDS ${UI_OUTPUT_DIR}/${fnt}.rcc DEPENDS ${deps2} )
    add_dependencies(my_target_name ${fnt}_tgt)
endforeach()

字符串

相关问题