I've got two executables both of which need to be linked to N libraries which are the same:
add_executable(MyExe1 main1.cpp)
add_executable(MyExe2 main2.cpp)
target_link_libraries(MyExe1 lib1 lib2 lib3 ... libN)
target_link_libraries(MyExe2 lib1 lib2 lib3 ... libN)
So I have to write target_link_libraries
twice; once for MyExe1
and once for MyExe2
. Is there any way to shorten the way some common libraries are linked to two different executables? I am wondering if it's possible to link lib1
... libN
libraries to both MyExe1
and MyExe2
in one command to avoid redundancy and make the CMake file cleaner.
You can use the set
command to set a variable from a list of arguments:
add_executable(MyExe1 main1.cpp)
add_executable(MyExe2 main2.cpp)
set(LIBS lib1 lib2 lib3 ... libN)
target_link_libraries(MyExe1 ${LIBS})
target_link_libraries(MyExe2 ${LIBS})
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