Guillaume Jacquenot
Guillaume Jacquenot

Reputation: 11717

Multiple static library inclusion in CMake TARGET_LINK_LIBRARIES

I have a CMake multiple definition linking problem with an executable that depends on a shared library that contains a static library.

I create a shared library foo that depends on a static library bar.

add_library(foo SHARED foo.cpp)
target_link_libraries(foo bar)

By definition, the content of bar is in library foo.

Then I create an executable exe that depends on foo.

add_executable(exe exe.cpp)
target_link_libraries(exe foo)

At linking time, I have a multiple definition warning/error that tells me that functions in library bar are present twice. When looking at the linking command, I see that exe is linked against bar and foo, which is not consistent.

Do I miss something in the declaration of dependencies? Do I miss a magic CMake keyword?

Upvotes: 7

Views: 6599

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69922

Like this:

add_library(foo SHARED <foo source files>)
target_link_libraries(foo PRIVATE bar)

If other libraries are linked against foo, make sure to use CMake keywordPRIVATE,PUBLIC or INTERFACE

Upvotes: 8

Related Questions