Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake and target_link_libraries on library depending on another lib

Tags:

c++

cmake

I am using CMake to build different C++ libraries, the whole thing can be summed up like this :

  • lib a : depends on nothing
  • lib b : depends on a

I now need to create a lib c that depends on b. Do I need to link c only on b ? or on b and a because b depends on a ?

target_link_libraries(c b) or target_link_libraries(c b a) ?

Thanks

like image 522
MartinMoizard Avatar asked Aug 07 '14 15:08

MartinMoizard


People also ask

How does CMake target_link_libraries work?

target_link_libraries is probably the most useful and confusing command in CMake. It takes a target ( another ) and adds a dependency if a target is given. If no target of that name ( one ) exists, then it adds a link to a library called one on your path (hence the name of the command).

Can target_link_libraries be called multiple times?

There is negligible performance penalty for having two separate target_link_libraries calls.

What does CMake Add_library do?

Adds a library target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project. The actual file name of the library built is constructed based on conventions of the native platform (such as lib<name>.


1 Answers

In your code building library b, you should tell CMake that b depends on a:

target_link_libraries(b a)

Then, your library/application c can link to only what it uses and not have to worry about dependencies of dependencies:

target_link_libraries(c b)

Library a will be pulled in for you.

like image 55
George Hilliard Avatar answered Oct 04 '22 22:10

George Hilliard