0

I am working on a larger library, which we now want to build with cmake.

The structure is somewhat like this:

foo/
- CMakeLists.txt
- include/
-- foo/
--- utils/
---- utils.hpp
--- foo.hpp
- src/
-- utils/
--- utils.cpp
-- foo.cpp

And the root CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.15)

project(foo CXX)

target_include_directories(
  foo
  PUBLIC
    include
  PRIVATE
    src
)

target_sources(
  foo
  PRIVATE
    src/foo.cpp
    src/utils/utils.cpp
)

The problem im seeing now is that the target_sources() call will become very unreadable and error prone the more source files are added.

Ive read about GLOB but also read that it is bad practice nowadays.

Whats solutions do you normaly use for libraries like this?

Symlink
  • 383
  • 2
  • 12
  • `target_sources` can be used multiple times, so you can group sources in preferred manner. Some time ago I used this to create [target_optional_sources function](https://stackoverflow.com/a/52589309/1387438) with a great results.. – Marek R Feb 16 '21 at 12:23
  • 2
    The solutions are to GLOB or to not GLOB. Make your choice. – eerorika Feb 16 '21 at 12:31
  • https://stackoverflow.com/q/1027247/1387438 – Marek R Feb 16 '21 at 12:38
  • 1
    You could list sources in variables. (This also comes in handy, if you want to use different `source_group`s for them.) `set(SOURCES src/foo.cpp ...) set(SOURCES_UTIL src/utils/utils.h ...) target_sources(foo PRIVATE ${SOURCES} ${SOURCES_UTIL})`. Furthermore cmake outputs the name of a source it cannot find, so fixing the source names is trivial. – fabian Feb 16 '21 at 19:01

0 Answers0