-2

I want to build my C++ project with CMake and I want to include automatically every new file on "cmake ." my project structure is:

Application/ Graphics/ CMakeLists.txt CMakeLists.txt.user main.cpp

./Application: CMakeLists.txt Logger/ Recovery/ application.cpp application.hpp firstclass.cpp firstclass.hpp singleton.hpp

./Application/Logger: CMakeLists.txt logger.cpp logger.hpp

./Application/Recovery: CMakeLists.txt recovery.cpp recovery.hpp

./Graphics: CMakeLists.txt drawableobject.cpp drawableobject.hpp graphics.cpp graphics.hpp

Each folder has own CMakeLists.txt

I did so far this in master CMake:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Asteri VERSION 1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
message("Source dir: ${SOURCE_DIR}")

#file(GLOB_RECURSE SRC_FILES ${SOURCE_DIR}/*.cpp)
#file(GLOB_RECURSE HEADER_FILES ${HEADER_DIR}/*.hpp)


set(PROJECT_NAME "Asteri")

macro(SUBDIRLIST result curdir)
  file(GLOB children RELATIVE ${curdir} ${curdir}/*)
  set(dirlist "")
  foreach(child ${children})
    if(IS_DIRECTORY ${curdir}/${child})
      list(APPEND dirlist ${child})
    endif()
  endforeach()
  set(${result} ${dirlist})
endmacro()

SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})

foreach(subdir ${SUBDIRS})
message("Subdirectory: ${subdir}")
  add_subdirectory(${subdir})
endforeach()

add_executable(${Asteri} main.cpp)
  1. The question is how to connect all pieces together?
  2. What I need in other CMakeLists.txt?
  3. How to communicate children -> parent or I misunderstood the concept of CMake?
Pablo
  • 13,271
  • 4
  • 39
  • 59
dexzoyp
  • 21
  • 5
  • Does this answer your question? [How to use all \*.c files in a directory with the Cmake build system?](https://stackoverflow.com/questions/2110795/how-to-use-all-c-files-in-a-directory-with-the-cmake-build-system) – Alex Reinking Apr 19 '22 at 01:39
  • Globbing your source files is a really, really bad practice. You will gobble up any "proof of concept" sources that may still be around, as well as any source files you'd rather compile only when a certain option is set. I understand the desire to "compile everything", especially to a beginner. But it really, really comes back to bite you sooner or later. And once a project is out of its fledgeling state, the number of source files added to it sharply declines anyway. I would suggest having an explicit list of source files to compile, and adding to that just as explicitly. – DevSolar Apr 19 '22 at 10:58

2 Answers2

0

No need for other CMakeLists.

I guess you want something like this in top CMakeLists:

cmake_minimum_required(VERSION 3.12)
project(Asteri VERSION 1.0)

file(GLOB_RECURSE ASTERI_SRC_FILES CONFIGURE_DEPENDS
    "${PROJECT_SOURCE_DIR}/Application/*.cpp"
    "${PROJECT_SOURCE_DIR}/Graphics/*.cpp"
)
add_executable(Asteri main.cpp ${ASTERI_SRC_FILES})

It's worth noting that GLOB/GLOB_RECURSE -even with CONFIGURE_DEPENDS- is bad practice: it's slow, specially on Windows from my experience, and may not work as expected depending on generator used.

SpacePotatoes
  • 659
  • 5
  • 11
0

I found out the answer to my problems.

set(SOURCES "${PROJECT_DIR}/main.cpp" CACHE INTERNAL STRINGS)
add_subdirectory(application)
FOREACH(it ${SOURCES})
    message("source file: ${it}")
ENDFOREACH()

add_executable(Asteri ${SOURCES})

In this way I store main.cpp path into variable SOURCES You can see your variables after 'make' in CMakeCache.txt.

After this is done, my variable SOURCES looks like:

/STRINGS
SOURCES:INTERNAL=/home/default/cpp_testing_project/cmake_project/source/main.cpp;

After that in application subdirectory I have CMakeLists.txt:

cmake_minimum_required(VERSION 3.7)

# Get source files
file(GLOB CPP_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.c")

set(SOURCES ${SOURCES} ${CPP_LIST} CACHE INTERNAL STRINGS)

And now SOURCES looks like:

SOURCES:INTERNAL=/home/default/cpp_testing_project/cmake_project/source/main.cpp;/home/default/cpp_testing_project/cmake_project/source/application/application.cpp;/home/default/cpp_testing_project/cmake_project/source/application/drawableobject.cpp;/home/default/cpp_testing_project/cmake_project/source/application/firstclass.cpp;/home/default/cpp_testing_project/cmake_project/source/application/graphics.cpp;/home/default/cpp_testing_project/cmake_project/source/application/application.hpp;/home/default/cpp_testing_project/cmake_project/source/application/drawableobject.hpp;/home/default/cpp_testing_project/cmake_project/source/application/firstclass.hpp;/home/default/cpp_testing_project/cmake_project/source/application/graphics.hpp;
dexzoyp
  • 21
  • 5