5

I'm trying to create rcc files with CMake, using the qt5_add_binary_resources(target inputfile ... OPTIONS ... DESTINATION ...) macro:

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

Since I'm using other macros such as add_executable in my CMakeLists file (which asks for a target), I'm getting the following error:

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 [..]

Note that my CMakeLists file is nested in a root CMakeLists.

EDIT: I took a look at the definition (l.226) of the qt5_add_binary_resources macro. The last line is the command provoking my error. It doesn't seem to be doing anything:

add_custom_target(${target} ALL DEPENDS ${rcc_destination})

I don't understand why the macro needs a target?

EDIT: here's the content of my CMakeLists.txt, though simplified for clarity reasons.

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()

According to this file, I'm trying to generate two rcc files: redTheme.rcc and blueTheme.rcc.

Grégoire Borel
  • 1,880
  • 3
  • 33
  • 55
  • Can you just change target file name for resource file? `qt5_add_binary_resources(resources "qml.qrc" OPTIONS ARGS -binary)` or something. And in the code of your application: QResource::registerResource("resources.rcc"); – Tony O Mar 21 '16 at 12:44
  • No I can't. I'm getting the same error: another `target` already exists. – Grégoire Borel Mar 21 '16 at 12:59
  • Can you provide more code from your CMakeList file? – Tony O Mar 21 '16 at 13:03
  • See updated original post. Note that this file is itself embedded within a root `CMakeLists` file (which is only setting variables and adding subdirectories). – Grégoire Borel Mar 21 '16 at 13:19
  • In my case replacing FOREACH with `qt5_add_binary_resources("redTheme" "themes/redTheme.qrc" OPTIONS ARGS -binary)` works fine. And after running cmake I got `redTheme.rcc` file in the same directory as the myApp binary. – Tony O Mar 21 '16 at 13:29
  • `target` in qt5_add_binary_resources is just an argument name of this function. – Tony O Mar 21 '16 at 13:34
  • You're right. However doing the same does not produce anything in my project. Two targets are created in my build configuration: http://i.imgur.com/1pGwXi0.png Running `CMake` does not create the `rcc` files. My binary is located in the `Release` directory, could that be a problem (I'm using two build configurations)? – Grégoire Borel Mar 21 '16 at 16:47
  • So, now cmake with qt5_add_binary_resources runs without an error? – Tony O Mar 21 '16 at 17:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106948/discussion-between-gregoire-borel-and-tony-o). – Grégoire Borel Mar 21 '16 at 20:08

1 Answers1

0

By the way... you don't need to pass "OPTIONS ARGS -binary" as it is defined already as binary. You need to pass a target name because you can call "make targetname" to generate rcc file manually. Also the rcc file will be generated by "all" target and if you add it as a dependency to another target with add_dependeny. The rcc file won't be generated at cmake configure time but on compile time. Also it will be re-generated if a file changed.

So you need to pass an unused target name as the error told you!

Wutz
  • 1