[CMake] cmake sometimes don't find the header

P F pfultz2 at yahoo.com
Sun Nov 12 20:27:54 EST 2017


I am not sure, but you should write the cmake target-oriented(instead of using variables). So you shouldn’t use `include_directories` either. It should be written:

#
# Build static library
add_library(database database.cpp database.h)

target_compile_features(database PUBLIC cxx_defaulted_functions)
target_include_directories(database PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(database record spectogram)

And record and spectogram should be written in a similar fashion. By using targets, you don’t have to worry about the targets being defined yet, so the superproject doesn’t need to order the `add_subdirectory` calls(it can even support circular dependencies).

Another thing, you shouldn’t declare another project unless you planning on making it standalone. This means it should find the dependencies through `find_package` and be installable with its usage requirements, which would be written like this:

project(database)

find_package(record)
find_package(spectogram)

#
# Build static library
add_library(database database.cpp database.h)

target_compile_features(database PUBLIC cxx_defaulted_functions)
target_include_directories(database PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(database record spectogram)

install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/database.h DESTINATION include)

install(TARGETS database EXPORT database-targets
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    INCLUDES DESTINATION include
)

install(EXPORT database-targets
    FILE database-targets.cmake
    DESTINATION lib/cmake/database
)

file(WRITE "${PROJECT_BINARY_DIR}/database-config.cmake" "
include(CMakeFindDependencyMacro)
find_dependency(record)
find_dependency(spectogram)
include(\"\${CMAKE_CURRENT_LIST_DIR}/database-targets.cmake\")
")

write_basic_package_version_file("${PROJECT_BINARY_DIR}/database-config-version.cmake"
    VERSION 1.0
    COMPATIBILITY AnyNewerVersion
)

install(FILES
    "${PROJECT_BINARY_DIR}/database-config.cmake"
    "${PROJECT_BINARY_DIR}/database-config-version.cmake"
    DESTINATION lib/cmake/database
)

The dependencies can be found either through find_package for standalone or through add_subdirectory in the superproject by overriding find_package. The effective cmake talk describes how such setup works, here:

https://www.youtube.com/watch?v=bsXLMQ6WgIk <https://www.youtube.com/watch?v=bsXLMQ6WgIk>

Or you can use a single project for all components.

> On Nov 12, 2017, at 8:04 AM, Carlton Banks <noflaco at gmail.com> wrote:
> 
> I am not sure I understand why I am having this problem..
> but cmake seem to have some problems with finding the header file located in a different project. 
> 
> tree view of my system: https://pastebin.com/AvydEbeW
> 
> I’ve included the directory to the project which header files I am interested in, and also added the path in my 
> target_inlcude_directories. The problems is my database.h cannot see record.h.
> 
> database/CmakeLists.txt:
> 
> https://pastebin.com/DpcMjtMa
> 
> The weird part is that it sometimes is able to compile, and other times it get stuck with this not able to find the header?
> 
> what could the problem be?
> 
> 
> 
> 
> -- 
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20171112/cfb5ce57/attachment.html>


More information about the CMake mailing list