Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to compile based on different main entries

Tags:

c++

cmake

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.

like image 224
B Faley Avatar asked Nov 18 '12 13:11

B Faley


1 Answers

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.

like image 93
Fraser Avatar answered Oct 26 '22 17:10

Fraser