John M. John M. - 11 days ago 5
C++ Question

Can I create a single file that mentions all files I want to build : c++

In React, I can write code like this for my containers/components:

export App from './App/App';
export Chat from './Chat/Chat';
export Home from './Home/Home';


This allows me to specify the stuff that I am exporting from that dir. Can I do something like that in
CMake
?

What I want is to be able to create a header file that includes the files that I want to build. I dont want to list them out in
CMakeLists.txt
as it becomes too cluttered. I also dont want to
GLOB_RECURSE
as it doesnt allow me to select files. How do I do this?

Answer

Just create file which lists sources:

sources.list:

foo.c
bar/baz.c

and read it using file(STRINGS) command into the variable:

CMakeLists.txt:

# Load list of sources into 'sources' variable
file(STRINGS "sources.list" sources)

# Use the variable
add_executable(my_exe ${sources})
Comments