How can I add several source files to ALL CMake add_executable()
s (aka. targets)?
I am just writing something like an automated CMake script, so I have no prior knowledge of any target names (or variables) defined in normal projects.
I know everything about the source files that I want to add.
My script will be
include()
ed toCMakeLists.txt
in other normal projects.
Is there an elegant way to do this besides iterating through BUILDSYSTEM_TARGETS
and target_sources()
for each target?
I have seen solutions that add sources files to some specific target: Can one add further source files to an executable once defined?
To elaborate on what I want to do, check this:
# my_script.cmake
some_magic_function_that_adds_sources_to_all_targets(
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
# CMakeLists.txt (of some other projects)
cmake_minimum_required(VERSION 3.7)
project(project_name)
include(abs_path_to_my_script.cmake) # can be anywhere in this file
add_executable(exe1
exe1.cpp)
add_executable(exe2
exe2.cpp)
add_executable(i_dont_know_exec_name
i_dont_know.cpp
how_many_files.cpp
are_here.cpp)
As a result, the CMakeLists.txt
will be functionally equivalent to:
# CMakeLists.txt (of some other projects)
cmake_minimum_required(VERSION 3.7)
project(project_name)
add_executable(exe1
exe1.cpp
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
add_executable(exe2
exe2.cpp
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
add_executable(i_dont_know_exec_name
i_dont_know.cpp
how_many_files.cpp
are_here.cpp
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)