Is there a way to automatically set the add_executable’s target name to it’s source file name in CMake?

  Kiến thức lập trình

I’m quite new to CMake. I want to get the executable for some CPP files with their filename as their executable filename. For example output1.cpp gives the executable named output1.
Currently I’m using the following lines in my CMakeLists.txt file.

add_executable(output1 output1.cpp)
add_executable(output2 output2.cpp)
add_executable(output3 output3.cpp)
.
.
.    
add_executable(outputN outputN.cpp)

I know there is a for loop solution for this. Like,

foreach(i RANGE 1 N)
    add_executable(output${i} output${i}.cpp)
endforeach()

But that’s not what I’m looking for. I’m looking for a solution that does not require passing hardcoded target names. Is there a way to achieve this?

LEAVE A COMMENT