Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake generate list of source files without glob

I'm currently using this piece of code to generate my list of source files for CMake to compile my C++ project:

file(GLOB CPP_FILES src/*.cpp)

As I have read here that this is discouraged by CMake I would like to know if I can list the source files explicitly for CMake using CMake, as I am not familiar with scripting languages like Python.

I'm asking this as it would require a lot of work to add all the cpp files manually into CMake, especially when working with multiply people.

The project is platform-independent and my source files are in a sub-folder.

like image 604
ShadowDragon Avatar asked Jul 13 '17 21:07

ShadowDragon


1 Answers

There is no native method for CMake to do this for you.

What you can do for large or shared projects is use a script, that could be created by anything that can scan the filesystem or some other repository, to generate a CMake file that lists the source files.

Then you can just include() this generated CMake file.

Instructions in the CMake file could be using target_sources() for a known target.

CMakeLists.txt:

add_executable(myexe "")
include(sourcelist)

sourcelist.cmake:

target_sources(myexe PRIVATE
    mysourcefile1.cpp
    mysourcefile2.cpp
)

Or via appending to a variable:

CMakeLists.txt:

include(sourcelist)
add_executable(myexe ${sources})

sourcelist.cmake:

set(sources ${sources} 
    mysourcefile1.cpp
    mysourcefile2.cpp
)
like image 127
utopia Avatar answered Sep 19 '22 05:09

utopia