Search code examples
c++makefilecmakeglob

CMake shell find


I'm trying to import an external project from an existing project that uses CMakeFiles. For this, I'm trying to add a static library ".a" with the files that I need for imports.

My CMakeFiles.txt:

cmake_minimum_required(VERSION 2.8.9)                                                                                                                                               
project(TEST)                                                                                                                                                                        
set(CMAKE_BUILD_TYPE Release)                                                                                                                                                       

#Bring the headers, such as Student.h into the project                                                                                                                              
include_directories(include)                                                                                                                                                        

#However, the file(GLOB...) allows for wildcard additions:

file(GLOB SOURCES "src/examples/equality_prob/*.cpp" "src/examples/equality_prob/common/*.cpp")
#Generate the static library from the sources                                                                                                                                       
add_library(testEquality STATIC ${SOURCES})    

Until there I think that I'm in the right way. But in the file(GLOB SOURCES , I want to add all the files *.cpp , *.h and so on, of a folder that has other folders inside them.

I can do this in Makefiles with something like this:

$(shell find ${SRC}/examples -type f -name '*.o')  

but how can I make this in CMakeFiles?


Solution

  • Using GLOB_RECURSE instead may give you the results you want.

    BUT

    This is not a recommended approach to collect source files, from GLOB documentation (also valid for GLOB_RECURSE):

    We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

    So, although a bit inconvenient it is better to explicitly list the files that make up the library.