I have in my CMakeLists.txt
:
add_custom_command(OUTPUT chattr.tab.cpp
DEPENDS table_gen
COMMAND ./table_gen > chattr.tab.cpp
)
This puts the generated source file chattr.tab.cpp
in the object directory (which is as I want it). However, as a result a #include "someheader.h"
inside chattr.tab.cpp
doesn't find that header, which is in the source directory.
This generated file is only added to some targets. Is there a way to cause some include directory to be added when compiling this generated source file (to CMakeFiles/sometarget.dir/chattr.tab.cpp.o
), and only when compiling that source file? As opposed to adding the include directory to all targets that use it? The latter would require to add multiple target_include_directories()
and would cause the include to be used for every source file of that target, not just the generated source file.
EDIT:
As per squareskittles answer, I produced the following CMakeLists.txt file (only showing the bottom part):
....
#=================================================================
# GENERATED SOURCE FILES
#
add_executable(table_gen table_gen.cxx)
add_custom_command(OUTPUT chattr.tab.cpp
DEPENDS table_gen
COMMAND ./table_gen > chattr.tab.cpp
)
add_custom_command(OUTPUT PgnGrammar.h
DEPENDS generate_PgnGrammar.h.sh
COMMAND ${CMAKE_CURRENT_LIST_DIR}/generate_PgnGrammar.h.sh
)
set(GENERATED_SOURCES chattr.tab.cpp PgnGrammar.h)
set_source_files_properties(chattr.tab.cpp PROPERTIES
INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}" # For local header files.
)
#=================================================================
# TEST EXECUTABLES
#
add_executable(tstchessposition tstchessposition.cxx)
target_link_libraries(tstchessposition PRIVATE AICxx::cwchessboard AICxx::cwds)
add_executable(tstbenchmark tstbenchmark.cxx)
target_link_libraries(tstbenchmark PRIVATE AICxx::cwchessboard AICxx::cwds)
add_executable(tstpgnread tstpgnread.cxx PgnDatabase.cxx MemoryBlockList.cxx ${GENERATED_SOURCES})
target_link_libraries(tstpgnread PRIVATE AICxx::cwchessboard AICxx::cwds)
add_executable(tsticonv tsticonv.cxx)
target_link_libraries(tsticonv PRIVATE PkgConfig::glibmm)
add_executable(tstpgn tstpgn.cxx PgnDatabase.cxx MemoryBlockList.cxx ${GENERATED_SOURCES})
target_link_libraries(tstpgn PRIVATE AICxx::cwchessboard AICxx::cwds)
add_executable(tstspirit tstspirit.cxx PgnGrammar.h)
target_include_directories(tstspirit PUBLIC "${top_objdir}")
But this results in:
[ 51%] Generating chattr.tab.cpp
./table_gen > chattr.tab.cpp
[ 54%] Generating chattr.tab.cpp
./table_gen > chattr.tab.cpp
Apparently, because both, tstpgnread
and tstpgn
link with chattr.tab.cpp
, it is generated TWICE?! Is this a bug in CMake or am I doing something else wrong?