[CMake] Add dependencies to the automoc/autorcc target?

Yves Frederix yves.frederix+cmake at gmail.com
Tue Jan 17 06:02:17 EST 2017


Hi all,

I have a situation in which I would need to define a dependency for
the automoc target. Is it possible to do this somehow?

Basically, I have a setup in which a resource that is to be embedded
is generated by another target. However, due to parallelism in the
build and the lack of a dependency of the _automoc target on this
target, I often get an error like :

   RCC: Error in '.../build/resources.qrc': Cannot find file
'../build/myfile.txt'

I tried adding a dependency using add_dependencies, but this doesn't
seem to work.

Minimal example code is below (you will need to create an empty
main.cpp though). I am using CMake 3.7.1 with a VS generator. My build
command looks like 'cmake --build . -- -m'.


cmake_minimum_required(VERSION 3.7)

project(test CXX)

find_package(Qt5Quick REQUIRED)

set(CMAKE_AUTORCC ON)

# Define target that generates some files.
add_custom_target(generate_files)
add_custom_command(TARGET generate_files COMMAND sleep 1) # Wait a bit...
add_custom_command(TARGET generate_files COMMAND ${CMAKE_COMMAND} -E touch
  ${CMAKE_BINARY_DIR}/myfile.txt)

# Write the .qrc file for embedding these generated files.
file(WRITE ${CMAKE_BINARY_DIR}/resources.qrc
  "<!DOCTYPE RCC><RCC
  version=\"1.0\">\n<qresource>\n<file>${CMAKE_BINARY_DIR}/myfile.txt</file>\n</qresource>\n</RCC>")

add_library(foo SHARED main.cpp ${CMAKE_BINARY_DIR}/resources.qrc)
add_dependencies(foo generate_files)
target_link_libraries(foo PRIVATE Qt5::Quick)


Thanks!

Regards,
Yves


More information about the CMake mailing list