Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake -- Add all sources in subdirectory to cmake project

Tags:

c++

cmake

As a follow up to this question: Add Source in a subdirectory to a cmake project

What is the best way (perhaps using the FILE directive?) to select all the .cpp and .h files in the subdirectory and add them to the SOURCE variable defined in the parent directory?


Example from answer to question above:

set(SOURCE
  ${SOURCE}
  ${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
  ${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
  PARENT_SCOPE
)

set(HEADERS
   ${HEADERS}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
   PARENT_SCOPE
)

Is it possible to do something like this?

FILE(GLOB SUB_SOURCES *.cpp)
set(SOURCE
  ${SOURCE}
  ${CMAKE_CURRENT_SOURCE_DIR}/${SUB_SOURCES}
  PARENT_SCOPE
)

What is the best way (using CMake) to compile all the sources in a directory and a subdirectory into a single output file (not multiple libraries?)

like image 468
therealrootuser Avatar asked Aug 01 '14 03:08

therealrootuser


People also ask

How do I add a library to CMake project?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories. In this case, we will create a subdirectory specifically for our library.

What does add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.


1 Answers

I think what you are looking for is the aux_source_directory command.

aux_source_directory Find all source files in a directory.

aux_source_directory( )

Collects the names of all the source files in the specified directory and stores the list in the provided. This command is intended to be used by projects that use explicit template instantiation. Template instantiation files can be stored in a "Templates" subdirectory and collected automatically using this command to avoid manually listing all instantiations.

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.

Your CMakeLists.txt within the subdirectory could look like this:

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SUB_SOURCES)

set(SOURCE
  ${SOURCE}
  ${SUB_SOURCES}
  PARENT_SCOPE
)

The recommended practice is however, as you see from the documentation, to list the files individually within CMakeLists.txt as changes to the CMakeLists.txt file triggers running cmake.

I hope this was helpful and to the point.

like image 192
misoboute Avatar answered Oct 20 '22 09:10

misoboute