2

I have the problem that add_custom_command is always out of date and therefore runs on every build. the custom command runs a tool that is a target of the same project to generate a file that is used by another target:

add_executable(GeneratorTool main.cpp)

add_custom_command(
    OUTPUT generated.h
    COMMAND GeneratorTool
    DEPENDS main.cpp
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "** GeneratorTool **"
)

add_library(MyLib STATIC generated.h ...)

In the build output (visual studio 2010) I always see ** GeneratorTool **. I would expect that it does not build again once generated.h exists and is newer than main.cpp. Any ideas?

Thanks, Jochen

4

2 回答 2

2

First of all, you can put DEPENDS on GeneratorTool in your add_custom_command instead of main.cpp . GeneratorTool already depends main.cpp .

Then most likely it is the location of generated.h which is ambiguous which forces the rebuilt of generated.h.

Make sure that MyLib looks for the generated.h in the right place.

My blind guess is to try:

add_library(MyLib STATIC ${CMAKE_CURRENT_SOURCE_DIR}/generated.h ...)
于 2011-12-22T11:23:51.717 回答
1

Another thing it could be - make sure the command is generating all OUTPUT files. If you generator is failing to generate even one of them, it will run every time (this was my problem).

于 2018-04-18T01:18:06.940 回答