Replace file GLOB for a better way to add include files

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:

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.

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

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.

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.

1 Like

Thank you.