Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Linking multiple libraries to two executables in one command

Tags:

cmake

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.

like image 252
B Faley Avatar asked Feb 12 '13 11:02

B Faley


1 Answers

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})
like image 169
Oskar N. Avatar answered Oct 21 '22 21:10

Oskar N.