Question asked at: https://stackoverflow.com/questions/75546798/file-generated-by-add-custom-command-cannot-be-found-within-add-subdirectory
I'm in the process of generating an api using flatbuffers within cmake. Now when on top level of the cmake directory tree the api is generated, the include files that should be generated are not found when they are needed in the branches.
./CMakeLists.txt
set(INTERFACE_FBS ${CMAKE_CURRENT_SOURCE_DIR}/fbs/testapi.fbs)
flatbuffers_generate_headers(TARGET incl
SCHEMAS ${INTERFACE_FBS}
FLAGS --scoped-enums --cpp-std c++17)
target_link_libraries(incl INTERFACE flatbuffers::flatbuffers)
set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES incl)
#add_executable(testapp app/src/test.cpp)
#target_link_libraries(testapp incl)
add_subdirectory(app)
./app/CMakeLists.txt
add_executable(app src/test.cpp)
target_link_libraries(app incl)
Running the system like this results in the following error:
CMake Error at app/CMakeLists.txt:2 (add_executable):
Cannot find source file:
/home/xxx/build.test/incl/testapi_generated.h
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
The testapi_generated.h
can't be found as it's not generated. But it should be generated as the function flatbuffers_generate_headers
sets an add_custom_command
for the wanted .h
file.
When I move the building of the executable to the main cmake file, so replacing the bottom of: ./CMakeLists.txt
add_executable(testapp app/src/test.cpp)
target_link_libraries(testapp incl)
#add_subdirectory(app)
the .h
file is generated and the simple application builds without problems. Also enabling the subdirectory at that point will result in a working executable. Performing a make clean and the building will fail again.
What am I doing wrong? Is it not possible to first use files created by an add_custom_command in a subdirectory?