Reputation: 56567
I tried my best to add a custom cmake target that compiles a list of .cpp files from a specific folder, and produces an executable for each file, with no success. Basically my scenario is as following:
foreach (FILE ${EXAMPLE_FILES})
add_executable(${TARGET_NAME} ${FILE})
endforeach ()
Currently, when I type cmake .. && make
, everything works fine, all those example files from the list ${EXAMPLE_FILES}
are compiled, and an executable produced for each. However, I'd like to remove this from the make all
target, i.e., I'd like to produce those executables only when I type e.g. make examples
. Any idea how to achieve this?
Upvotes: 0
Views: 853
Reputation: 66061
By adding EXCLUDE_FROM_ALL
keyword to add_executable
call, the building of the executable will be excluded from make all
. For make a single target to create multiple executables just make this target dependent from these executables.
# Create the custom target before loop.
add_custom_target(examples)
foreach (FILE ${EXAMPLE_FILES})
# Every executable target should have unique name.
# E.g. if every example source is given in form '<name>.c'
# Then we could use '<name>' as the executable target
string(REPLACE ".c" "" TARGET_NAME "${FILE}")
# Create an executable
add_executable({$TARGET_NAME} EXCLUDE_FROM_ALL ${FILE})
# And make 'examples' to depend from that executable
add_dependencies(examples ${TARGET_NAME})
endforeach ()
Upvotes: 1