# Header-only library

**URL:** https://discourse.cmake.org/t/header-only-library/5476
**Category:** Code
**Created:** [April 16, 2022, 5:58pm UTC](https://discourse.cmake.org/t/header-only-library/5476 "2022-04-16T17:58:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![exldna](https://discourse.cmake.org/user_avatar/discourse.cmake.org/exldna/32/2352_2.png) [@exldna](https://discourse.cmake.org/u/exldna)
#### Post date: [April 16, 2022, 5:58pm UTC](https://discourse.cmake.org/t/header-only-library/5476/1 "2022-04-16T17:58:52Z")

</div>

I have an h-o library named utils. I’m trying to include this library in my project. This is my folder structure:

```auto
+ my_proj
| + source
| | + ext
| | | + utils
| | | | + include
| | | | | + utils
| | | | | | patterns.hxx
| | | | | | other utils headers...
| | | | CMakeLists.txt
| | CMakeLists.txt
| | main.cxx
| | other sources...
| CMakeLists.txt

```

my\_proj/CMakeLists.txt:

```cmake
...
add_subdirectory("./source/")
...

```

my\_proj/source/CMakeLists.txt:

```cmake
...
add_executable(exe_name sources...)
...
add_subdirectory("./ext/utils")
target_link_libraries(exe_name utils)
...

```

my\_prog/source/ext/utils/CMakeLists.txt:

```auto
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project("utils"
    VERSION 0.0.1
    LANGUAGES CXX
)

set(UTILS_LIB_NAME "utils")

add_library(${UTILS_LIB_NAME} INTERFACE)

include(GNUInstallDirs)

target_include_directories(
    ${UTILS_LIB_NAME}
    INTERFACE 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/utils>
        $<INSTALL_INTERFACE:include/utils>
)

target_compile_features(
    ${UTILS_LIB_NAME} 
    INTERFACE 
        cxx_std_17
)

install(
    TARGETS ${UTILS_LIB_NAME} 
    EXPORT utils_Targets
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(
    DIRECTORY ${PROJECT_SOURCE_DIR}/include/utils 
    DESTINATION include
)

install(
    EXPORT utils_Targets
    FILE utilsTargets.cmake 
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/utils
    NAMESPACE utils::
)

include(CMakePackageConfigHelpers)

write_basic_package_version_file(
    "utilsConfigVersion.cmake"
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY AnyNewerVersion
)

configure_package_config_file(
    "${PROJECT_SOURCE_DIR}/cmake/utilsConfig.cmake.in"
    "${PROJECT_BINARY_DIR}/utilsConfig.cmake"
    INSTALL_DESTINATION
        ${CMAKE_INSTALL_LIBDIR}/cmake/utils
)

install(
    FILES 
        ${PROJECT_BINARY_DIR}/utilsConfig.cmake
        ${CMAKE_CURRENT_BINARY_DIR}/utilsConfigVersion.cmake 
    DESTINATION 
        ${CMAKE_INSTALL_LIBDIR}/cmake/utils
)

```

in the main I’m trying to include a header:

```auto
// main.cxx
# include <utils/patterns.hxx>

```

but, compiler say me:

```auto
"unable to open the utils/patterns.xxx inclusion file "

```

I build my project with this commands (from the my\_proj directory):

```auto
$ mkdir build && cd build && cmake ..
$ cmake --build . --config Release

```

so, what’s I do wrong?

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [April 17, 2022, 11:07pm UTC](https://discourse.cmake.org/t/header-only-library/5476/2 "2022-04-17T23:07:46Z")

</div>

If you’re including with a directory `utils/` then you should not add that directory to your include path, only the directory above:

```auto
target_include_directories(
    ${UTILS_LIB_NAME}
    INTERFACE 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)

```
