Search code examples
c++cmakedependenciesheader-files

Why would a CMake build not start build when header file changes


Below is a small sample of the way I'm building my project. I have a list of headers and a list of source files, and pass them to add_executable. Sometimes after several incremental builds, I'm changing the header file but the build isn't doing anything. The status shows that each target is checked but no changes are seen. If I do a small modification in the CPP file, then the build is executed.

What could be the cause of this?

list (APPEND ${PROJ_NAME}_SRC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/lua/lua_config.hpp)
list (APPEND ${PROJ_NAME}_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/source/lua/lua_config.cpp)
add_executable(${PROJ_NAME} ${${PROJ_NAME}_SRC_FILES} ${${PROJ_NAME}_SRC_HEADERS})

I'm using the 'Unix Makefiles' generator.

I see that all my projects header files are not part of the generated depend.cmake file. I guess this is the root of the problem. All the header files from the other conan packages are there, but not the ones for the top level project.


Solution

  • Two things are needed for the header files to be added to the depend.make file. First adding them in the list of files of the target, which I did and add the include directory using target_include_directories.

    target_include_directories(${PROJ_NAME} PRIVATE
         $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    )
    

    At this point it's still not working. This is because I'm passing two lists of files that are separated using a space ' ' character. After I joined the two lists into a single one using a ';' it started working.

    add_executable(${PROJ_NAME} "${${PROJ_NAME}_SRC_FILES};${${PROJ_NAME}_HEADER_FILES}")