# modern cmake file structure

**URL:** <https://discourse.cmake.org/t/modern-cmake-file-structure/1377>\
**Category:** Code\
**Created:** [June 12, 2020, 3:03pm UTC](https://discourse.cmake.org/t/modern-cmake-file-structure/1377 "2020-06-12T15:03:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kayarre/32/608_2.png) [@kayarre](https://discourse.cmake.org/u/kayarre)\
**Post date:** [June 12, 2020, 3:03pm UTC](https://discourse.cmake.org/t/modern-cmake-file-structure/1377/1 "2020-06-12T15:03:11Z")

</div>

I am trying to convert an existing cmake project to use some of the more modern cmake patterns and generate the correct Config.cmake files to be imported by another project

I have the following file structure in the one I want to update

```auto
./zoo/
    cmake/
        *.cmake.in
    libzoo/
        bind/
            CMakeLists.txt
            *.cpp
            *.h
            *etc.
        include/
            *.h
            libzoo/
              *.hpp
        src/
            *.cpp
        test/
            CMakeLists.txt
        CMakeLists.txt
    App/
         src/
         include/
         CMakeLists.txt

```

# Issues:

1. In the original code everything built and ran fine, but now I am getting errors saying that the **APP** can’t find the guile include headers now. How do I properly connect and inherit the header dependencies? I think its that modern cmake is more explicit, but I don’t know what I am missing.

Do I need to re-include the headers? meaning the app does not inherit the includes from the library?

```auto
target_include_directories(App
  PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
         $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
         ${Boost_INCLUDE_DIRS}
         ${PNG_INCLUDE_DIR}
         ${EIGEN_INCLUDE_DIRS}
         ${GUILE_INCLUDE_DIRS}
)

```

1. I want to be able to export a `ZooConfig.cmake` such that it can be found with `find_package` and would include **libzoo** , **zoo-guile** , and the **App**. There appears to be many paths, but I am having a hard time finding information out there that makes cohesive sense, and am really confused at this point.

The dependency chain is as follows:

- build a shared library from the source in `libzoo/src` with the header files in `libzoo/include` which depend on Boost, Eigen, PNG, and Threads called **libzoo**.

```auto
add_library(zoo SHARED
    ${zoo_sources}
)

add_library(zoo::core ALIAS zoo)

target_include_directories(zoo
  PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
         $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
         ${Boost_INCLUDE_DIRS}
         ${PNG_INCLUDE_DIR}
         ${EIGEN_INCLUDE_DIRS}
)

set(LIBS ${PNG_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} )
target_link_libraries(zoo PRIVATE ${LIBS})

set_target_properties(five
   PROPERTIES
   CXX_STANDARD "${ZOO_CXX_STANDARD}"
   CXX_STANDARD_REQUIRED "YES"
   CXX_EXTENSION "NO"
   INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib
)

install(
  TARGETS zoo 
  EXPORT LibZooTargets
  RUNTIME DESTINATION ${ZOO_INSTALL_BIN_DIR}
  LIBRARY DESTINATION ${ZOO_INSTALL_LIB_DIR}
  ARCHIVE DESTINATION ${ZOO_INSTALL_LIB_DIR}
)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
        DESTINATION ${ZOO_INSTALL_INCLUDE_DIR}
        FILES_MATCHING
          PATTERN "*.h"
          PATTERN "*.hpp"
)

if (BUILD_TESTS)
  add_subdirectory(test)
endif(BUILD_TESTS)

if (BUILD_GUILE_BINDINGS AND GUILE_FOUND)
    add_subdirectory(bind)
endif(BUILD_GUILE_BINDINGS AND GUILE_FOUND)

```

- build the test suite which depends on **libzoo**

```auto
set(SRCS ${test_srcs} )

set(LIBS zoo ${CMAKE_THREAD_LIBS_INIT} )

if (BUILD_GUILE_BINDINGS AND GUILE_FOUND)
    set(SRCS ${SRCS} guile.cpp)
    set(LIBS ${LIBS} zoo-guile)
endif(BUILD_GUILE_BINDINGS AND GUILE_FOUND)

add_executable(libzoo-test ${SRCS})

target_link_libraries(libzoo-test ${LIBS})
target_include_directories(libzoo-test PRIVATE .)

```

- build a guile binding library from `/libproject/bind` called **zoo-guile** depends on **libzoo**

```auto
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bundle.cpp
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/bundle.cmake
    DEPENDS libzoo-guile.cpp shapes.scm csg.scm transforms.scm text.scm util.scm vec.scm
            sandbox.scm ${CMAKE_CURRENT_SOURCE_DIR}/bundle.cmake)

add_library(zoo-guile SHARED ${CMAKE_CURRENT_BINARY_DIR}/bundle.cpp)

add_library(zoo::guile ALIAS zoo-guile)

add_dependencies(zoo-guile zoo)
target_link_libraries(zoo-guile zoo ${GUILE_LIBRARIES})

target_include_directories(zoo-guile
  PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
         $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
         ${GUILE_INCLUDE_DIRS}
)

set_target_properties(zoo-guile
   PROPERTIES
   CXX_STANDARD "${FIVE_CXX_STANDARD}"
   CXX_STANDARD_REQUIRED "YES"
   CXX_EXTENSION "NO"
   INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib
)

install(
  TARGETS zoo-guile 
  EXPORT LibZooTargets
  RUNTIME DESTINATION ${ZOO_INSTALL_BIN_DIR}
  LIBRARY DESTINATION ${ZOO_INSTALL_LIB_DIR}
  ARCHIVE DESTINATION ${ZOO_INSTALL_LIB_DIR}
  )

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        DESTINATION ${ZOO_INSTALL_INCLUDE_DIR}
        FILES_MATCHING
        PATTERN "*.h"
)

```

- build the **App** in which depends on **zoo-guile** in `App` directory.

```auto
# Instruct CMake to run moc, uic, and rrc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# Find the Qt libraries
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5Concurrent REQUIRED)

set(SRCS ${app_srcs})
set(QOBJECTS ${Qobj_srcs})

add_executable(App ${SRCS} ${QOBJECTS})
install(TARGETS App DESTINATION bin RENAME app)

target_include_directories(App
  PUBLIC 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
         $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
)

target_link_libraries(App
  PUBLIC
    zoo-guile
    Qt5::Widgets
    Qt5::Gui
    Qt5::OpenGL
    Qt5::Concurrent
    ${EXTRA_LIBS}
)

set_target_properties(App
   PROPERTIES
   CXX_STANDARD "${ZOO_CXX_STANDARD}"
   CXX_STANDARD_REQUIRED "YES"
   CXX_EXTENSION "NO"
   INSTALL_RPATH ${ZOO_INSTALL_LIB_DIR}
)

install(
  TARGETS App 
  EXPORT LibZooTargets
  RUNTIME DESTINATION ${ZOO_INSTALL_BIN_DIR}
  LIBRARY DESTINATION ${ZOO_INSTALL_LIB_DIR}
  ARCHIVE DESTINATION ${ZOO_INSTALL_LIB_DIR}
)

```

- Export the targets to a Config.cmake file from the project level CMakeLists.txt

```auto
#Skipping all the variable setting
 
add_subdirectory(libzoo)

```

This makes sense up until here when I try to export the target stuff

```auto
if(BUILD_APP AND GUILE_FOUND AND Qt5Core_FOUND)
    add_subdirectory(App)
endif(BUILD_APP AND GUILE_FOUND AND Qt5Core_FOUND)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    LibZooConfigVersion.cmake
    VERSION ${ZOO_VERSION}
    COMPATIBILITY AnyNewerVersion
)

install(EXPORT LibZooTargets
        DESTINATION ${ZOO_CMAKE_INSTALL_DIR}
)

configure_package_config_file(
	"cmake/LibZooConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/LibZooConfig.cmake"
	INSTALL_DESTINATION ${ZOO_CMAKE_INSTALL_DIR}
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/LibZooConfig.cmake"
        DESTINATION ${ZOO_CMAKE_INSTALL_DIR}
)

```

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kayarre/32/608_2.png) [@kayarre](https://discourse.cmake.org/u/kayarre)\
**Post date:** [June 12, 2020, 5:10pm UTC](https://discourse.cmake.org/t/modern-cmake-file-structure/1377/2 "2020-06-12T17:10:52Z")

</div>

I corrected one issue: I didn’t have the flag on to build the bindings…

🤦‍♂️

still having trouble with the installation config part.

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kayarre/32/608_2.png) [@kayarre](https://discourse.cmake.org/u/kayarre)\
**Post date:** [June 12, 2020, 6:58pm UTC](https://discourse.cmake.org/t/modern-cmake-file-structure/1377/3 "2020-06-12T18:58:22Z")

</div>

It appears I got it working.

---

<div class="post-metadata">

**Author:** ![phread](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phread/32/2285_2.png) [@phread](https://discourse.cmake.org/u/phread)\
**Post date:** [June 8, 2023, 5:53pm UTC](https://discourse.cmake.org/t/modern-cmake-file-structure/1377/4 "2023-06-08T17:53:03Z")

</div>

Did you update your original post with the corrections?
