Can one add further source files to an executable once defined?

Cmake

Cmake Problem Overview


Given I have defined an executable with its main source file in a CMakeList.txt file:

ADD_EXECUTABLE(MyExampleApp main.cpp)

Can I add further source files to this executable after this line but in the same or an included CMakeList.txt file?

Cmake Solutions


Solution 1 - Cmake

Use target_sources, available since cmake 3.1

eg. target_sources(MyExampleApp PRIVATE ${extra_file})

https://cmake.org/cmake/help/v3.1/command/target_sources.html

Solution 2 - Cmake

I think you may use:

add_executable(MyExampleApp main.cpp)
add_library(library STATIC ${ADDITIONAL_SOURCES})
set_target_properties(library PROPERTIES
     LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(MyExampleApp library)

Solution 3 - Cmake

It should be noted that for more recent versions of CMake (> 3.1 I think) one can append files to the SOURCES property on targets.

http://www.cmake.org/cmake/help/v3.3/prop_tgt/SOURCES.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTorbjörnView Question on Stackoverflow
Solution 1 - CmakeACyclicView Answer on Stackoverflow
Solution 2 - Cmakedizel3dView Answer on Stackoverflow
Solution 3 - CmakeJean-Michaël CelerierView Answer on Stackoverflow