Meysam
Meysam

Reputation: 18167

CMake: Linking multiple libraries to two executables in one command

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.

Upvotes: 10

Views: 8684

Answers (1)

Oskar N.
Oskar N.

Reputation: 8587

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})

Upvotes: 14

Related Questions