0

I am developing a c++ program and I want to generate a pair of source and header files from an xml file (for Qt DBus interface).

There are couple of problems:

  • If the files that I need to be generated are not generated beforehand, cmake complians that files are not found, I guess at add_executable command
  • I tried to create a dependency, but looks like my command add_custom_target does not work

Here is the part of cmake file:

add_custom_target(
        generateDBusHeaders
        COMMAND "${CMAKE_SYSROOT}/usr/bin/qdbusxml2cpp.exe \
        -c TestAdapter \
        -a ${CMAKE_CURRENT_SOURCE_DIR}/inc/testadapter.h : \
        ${CMAKE_CURRENT_SOURCE_DIR}/src/testadapter.cpp \
        ${CMAKE_CURRENT_SOURCE_DIR}/setup/com.example.test.xml"
)


add_executable(${PROJECT_NAME}
    src/main.cpp
    ## inc/testadapter.h src/testadapter.cpp ## If fiels not found, cmake gives error
)

# Add dependencies
# I expect this to generate files before add_executable
# But it looks like nothing being generated at all!
add_dependencies(${PROJECT_NAME} generateDBusHeaders) # Use qdbusxml2cpp

So the questions are:

  • How to deal with generating source files before calling add_executable
  • How to make sure that custom command qdbusxml2cpp.exe is being executed at all? I see no changes and no output in my cmake in terminal
DEKKER
  • 697
  • 4
  • 15
  • why do you need to guess? Doesnt cmake produce an error message? – 463035818_is_not_a_number Apr 05 '22 at 08:54
  • The [manual](https://cmake.org/cmake/help/latest/command/add_custom_target.html) says "use the `add_custom_command()` command to generate a file with dependencies". You should use `add_custom_command` instead of `add_custom_target`. – Jakob Stark Apr 05 '22 at 09:05
  • @JakobStark then how should I execute that command – DEKKER Apr 05 '22 at 09:11
  • @DEKKER why don't you go study the [example](https://cmake.org/cmake/help/latest/command/add_custom_command.html#examples-generating-files) on the manual page of `add_custom_command`? – Jakob Stark Apr 05 '22 at 09:14
  • In addition to the example I linked above, you may need to add the generated headers to the `BYPRODUCTS` directive of the `add_custom_command`. – Jakob Stark Apr 05 '22 at 09:21
  • @JakobStark Thanks, I got it working. maybe you want to add that as answer? – DEKKER Apr 05 '22 at 11:28
  • There are already answers to other, similar question. E.g. this one: https://stackoverflow.com/a/37862193/17862371 So maybe this can be closed as duplicated? – Jakob Stark Apr 05 '22 at 11:40
  • Or this one: https://stackoverflow.com/a/25698193/17862371 – Jakob Stark Apr 05 '22 at 11:43

0 Answers0