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

时间:2016-03-10 14:00:27

标签: qt cmake qml rcc

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

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

由于我在我的add_executable文件中使用CMakeLists等其他宏(要求target),我收到以下错误:<\ n / p>

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

1 个答案:

答案 0 :(得分:0)

顺便说一下......你不需要通过&#34; OPTIONS ARGS -binary&#34;因为它已被定义为二进制。 您需要传递一个目标名称,因为您可以调用&#34; make targetname&#34;手动生成rcc文件。此外,rcc文件将由&#34; all&#34;生成。目标,如果使用add_dependeny将其作为依赖项添加到另一个目标。 rcc文件不会在cmake配置时生成,但是在编译时生成。如果文件发生变化,也会重新生成。

因此,您需要传递未使用的目标名称,因为错误告诉您!