Suppose that I've got two main
entries, one in main1.cpp
and one in main2.cpp
(there are other files too, but only two main entries). How can I configure the CMakeLists.txt
file so that I can include either main1.cpp
or main2.cpp
based on different targets? i.e. I will eventually be able to use "make target1" to produce exec1
based on main1.cpp
and "make target2" to produce exec2
based on main2.cpp
along with other files.
Sounds like a case for making a library out of all your sources except the mains. Then just add two executable targets, each linked to the library.
add_executable(MyExe1 main1.cpp)
add_executable(MyExe2 main2.cpp)
add_library(MyLib <all the other files>)
target_link_libraries(MyExe1 MyLib)
target_link_libraries(MyExe2 MyLib)
You can just include all the sources twice, so MyExe1
would have everything except main2.cpp, and MyExe2
everything except main1.cpp, but this would involve compiling the sources twice and is inefficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With