1

How can I add several source files to ALL CMake add_executable()s (aka. targets)?

  1. 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.

  2. I know everything about the source files that I want to add.

  3. My script will be include()ed to CMakeLists.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
)
jerryc05
  • 454
  • 1
  • 4
  • 16
  • 1
    Intention for adding source for every target looks strange. E.g. would a `CMakeLists.txt` create any library and try to link with it, it will get "multiple definition" error. **Why** do you need that? It could be other ways, aside from adding sources, for achieve you purpose. E.g. if your expect `add_executable` to create a special type of application, and that type requires additional sources, then you may create another function/macro, which wraps `add_executable` call and adds sources. – Tsyvarev Nov 03 '20 at 07:55
  • @Tsyvarev My use case is, if you are familiar with compiler sanitizers, that I can embed sanitizer options into source code, and I want to do this for all my targets. See [this link](https://github.com/google/sanitizers/wiki/AddressSanitizerFlags#run-time-flags) – jerryc05 Nov 03 '20 at 21:44
  • 2
    You may create OBJECT library with additional sources, and specify that library with [link_libraries](https://cmake.org/cmake/help/latest/command/link_libraries.html) command (not to confuse with `target_link_libraries`). So all further executables will be automatically linked with that library and have its sources. – Tsyvarev Nov 03 '20 at 21:54
  • The easiest hack is to overload `add_executable` with your own function that adds your extra `.cpp` files as args. Once you do so, you have to access built-in operation as `_add_executable`. But, this is still a hack and one of CMake contributors recommends against it: https://crascit.com/2018/09/14/do-not-redefine-cmake-commands/ – R2RT Nov 03 '20 at 21:54

0 Answers0