11

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.

1 Answer 1

15

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}) 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to create a cmake pseudo-library that lets you do define lib1AndLib2 so you could use it in something like "target_link_libraries(MyExe1 lib1AndLib2)"?
@JamesMoore Yes, it is called an interface library, and you create it with add_library(MyInterfaceLibName INTERFACE).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.