3

I am trying to build a big project with CMake but I struggle on how to write the CMakeList.txt file. My project is separated in different folders each containing a set of .hpp and .cpp files more or less related together as follows:

root 

   - memory
   -- Memory.cpp
   -- Memory.hpp
   -- MemoryManager.hpp
   -- MemoryManager.cpp
   -- CMakeLists.txt

   - tools
   -- Array.cpp
   -- Array.hpp
   -- CMakeLists.txt

   - main.cpp
   - CMakeLists.txt

I would like to build all the files together to an executable. I don't want to build libraries in each subfolder as I don't see any good reason to do it. I would like also to avoid putting a single big list of all source files in the ADD_EXECUTABLE command of the CMakeLists.txt file located at the root of the project.

Do you have any idea of how to set this up correctly ?

Cheers,

M.

Nanoc
  • 33
  • 3

1 Answers1

4

You can use the GLOB function, such as:

file (GLOB _my_sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
  memory/*.cpp tools/*.cpp main.cpp)
add_executable (myprogbin ${_my_sources})
set_target_properties (myprogbin PROPERTIES OUTPUT_NAME myprog)

See http://cmake.org/cmake/help/cmake-2-8-docs.html#command:file for reference

Denis Arnaud
  • 388
  • 1
  • 4
  • 7
  • I was just wondering ... why the call to set_target_properties at the end? Is there any specific reason why you did that in this instance? (I am afraid that I might not be understanding something fundamental). – William Payne Mar 27 '13 at 16:47
  • set_target_properties just sets the name of the binary executable. You may not need it. I added it for completeness. – Denis Arnaud Apr 07 '13 at 17:21
  • file(GLOB) is strongly discouraged by the CMake authors, for source files. See https://stackoverflow.com/questions/32411963/why-is-cmake-file-glob-evil – David Faure Feb 14 '20 at 16:41