# Replace file GLOB for a better way to add include files

**URL:** <https://discourse.cmake.org/t/replace-file-glob-for-a-better-way-to-add-include-files/9947>\
**Category:** Code\
**Created:** [January 26, 2024, 9:40pm UTC](https://discourse.cmake.org/t/replace-file-glob-for-a-better-way-to-add-include-files/9947 "2024-01-26T21:40:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrendanL](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brendanl/32/4237_2.png) [@BrendanL](https://discourse.cmake.org/u/BrendanL)\
**Post date:** [January 26, 2024, 9:40pm UTC](https://discourse.cmake.org/t/replace-file-glob-for-a-better-way-to-add-include-files/9947/1 "2024-01-26T21:40:04Z")

</div>

I have a project that I put together from a tutorial on a website a few years ago. I have learned that I shouldn’t use GLOB to create a list of files for many reasons but I’m having trouble figuring out the correct way I should add them. Below I’ve included the entire CMakeLists.txt from a small example that compiles the library without error.

For context I’m using the Pitchfork layout and the directory looks like:

```auto
nrpp/
  build/
    <lots of cmake generated stuff>
  examples/
    badluk.cpp
  include/
    nrpp/
      nrpp.hpp
  src/
    flmoon.cpp
    julday.cpp

```

TLDR: How should I replace the first line in this CMakeLists.txt file to have a list of include files I can reuse the list instead of manually adding them in multiple places.

```auto
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${nrpp_SOURCE_DIR}/include/nrpp/*.hpp")

add_library(nrpp
    flmoon.cpp
    julday.cpp
    ${HEADER_LIST}
)

target_include_directories(nrpp PUBLIC "../include")
target_compile_features(nrpp PUBLIC cxx_std_20)
target_compile_options(nrpp PRIVATE -Wall -Wextra -pedantic -Wshadow)

source_group(
    TREE "${PROJECT_SOURCE_DIR}/include"
    PREFIX "Header Files"
    FILES ${HEADER_LIST}
)

```

Thannks!  
B

---

<div class="post-metadata">

**Author:** ![dnglaze](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/74df32/32.png) [@dnglaze](https://discourse.cmake.org/u/dnglaze)\
**Post date:** [January 27, 2024, 12:20am UTC](https://discourse.cmake.org/t/replace-file-glob-for-a-better-way-to-add-include-files/9947/2 "2024-01-27T00:20:37Z")

</div>

You can use globs if you want to, you just need to be aware of the pitfalls and tradeoffs you make by doing so. Just depends on your environment and requirements.

In the same way that the glob creates a list, you can just create a list.

```cmake
set(HEADER_LIST 
    "${nrpp_SOURCE_DIR}/include/nrpp/nrpp.hpp"
    # more headers, one per line or at least with a space between them
)

```

Rest of the code is left unchanged.

---

<div class="post-metadata">

**Author:** ![BrendanL](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brendanl/32/4237_2.png) [@BrendanL](https://discourse.cmake.org/u/BrendanL)\
**Post date:** [January 29, 2024, 6:21pm UTC](https://discourse.cmake.org/t/replace-file-glob-for-a-better-way-to-add-include-files/9947/3 "2024-01-29T18:21:33Z")

</div>

Thank you.
