Multiple static library inclusion in CMake TARGET_LINK_LIBRARIES
Asked Answered
W

1

6

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?

Webbed answered 15/12, 2014 at 18:15 Comment(2)
You need a sscce.orgLothair
the problem he highlights is a common one. Any cmake bittervet would have known what he meant :-)Alcott
A
7

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

Alcott answered 15/12, 2014 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.