Michael Aigner
Michael Aigner

Reputation: 5160

cmake target_link_libraries - unwanted target

I have a problem with cmake target_link_libraries.
I have 3 libs. The first is static compiled, the second one (shared lib) link to it and the third one is an executable which use the second lib.
My problem is that my first lib is automatically added to the third lib and leads into a "object already defined" problem.

Is it possible to hide away the first lib from the third one?

I use cmake 3.4.x Compiler: msvc 2010 x64

Thanks in advance
Tonka

Upvotes: 1

Views: 425

Answers (2)

Michael Aigner
Michael Aigner

Reputation: 5160

I've solved it. I can link to a library private, so f.e.

target_link_libraries(MyLib2 PRIVATE MyLib1)

will hide MyLib1 from everybody linking to MyLib2

Upvotes: 1

rubenvb
rubenvb

Reputation: 76509

Your third "lib" isn't a library, but an application. You need to add this using add_executable, not add_library.

If your shared library links in a static library, and then you want to link an application to both the static library and that shared library, you get two copies of the static library. Never link static libraries you plan to use elsewhere into a shared library, for this reason. Either make the first shared as well (the name implies that's what you want, as it is exactly what you are describing), or a workaround for this design problem could be to not explicitly link the application to the static library.

Upvotes: 1

Related Questions