# link two static libraries to a shared library

**URL:** https://discourse.cmake.org/t/link-two-static-libraries-to-a-shared-library/4132
**Category:** Code
**Tags:** os:windows
**Created:** [September 22, 2021, 9:19am UTC](https://discourse.cmake.org/t/link-two-static-libraries-to-a-shared-library/4132 "2021-09-22T09:19:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NePutin94](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neputin94/32/1809_2.png) [@NePutin94](https://discourse.cmake.org/u/NePutin94)
#### Post date: [September 22, 2021, 9:19am UTC](https://discourse.cmake.org/t/link-two-static-libraries-to-a-shared-library/4132/1 "2021-09-22T09:19:48Z")

</div>

The structure of my project:

```auto
+--ROOT
	-CmakeLists.txt
	+--imgui
		-CmakeLists.txt
	+--core
		-CmakeLists.txt
	+--imgui_glfw
		-CmakeLists.txt

```

Root CmakeLists.txt adds imgui, imgui\_glfw, and core as subdirectory:

```auto
...
add_executable(palka "main.cpp")
add_subdirectory(imgui_glfw)
add_subdirectory(imgui)
add_subdirectory(core)
target_link_libraries(palka Core)

```

CMakeLists for the core directory links imgui and imgui\_glfw:

```auto
...
add_library(Core SHARED ${CORE_SRC} ${HEADER_SOURCE})
target_link_libraries(Core PUBLIC IMGUI IMGUI_GLFW)

```

imgui CmakeLists:

```auto
...
file(GLOB IMGUI_SRC RELATIVE "${CMAKE_SOURCE_DIR}/palka_core/imgui" "*.cpp")
add_library(IMGUI STATIC ${IMGUI_SRC})
target_compile_definitions(IMGUI PUBLIC IMGUI_IMPL_OPENGL_LOADER_GLAD)
target_include_directories(IMGUI PUBLIC ".")

```

Imgui\_glfw has almost the same CMakeLists:

```auto
project(IMGUI_GLFW)
file(GLOB IMGUI_GLFW_SRC RELATIVE "${CMAKE_SOURCE_DIR}/imgui_glfw" "*.cpp")

find_package(glfw3 REQUIRED)
add_library(IMGUI_GLFW STATIC ${IMGUI_GLFW_SRC})
target_link_libraries(IMGUI_GLFW PRIVATE glfw IMGUI)
target_include_directories(IMGUI_GLFW PUBLIC ".")

```

The assembly of all the goals is successful, but the linking is .exe returns an error:

```auto
imgui/libIMGUI.a: in function `ImGui::TreePop(): multiple definition 
of `ImGui::TreePop() libPalkaCore.dll.a: first defined here

```

Apparently libIMGUI is linked not only for the Core target, but also for the palka target(the CMakeList list in the root directory). This can be avoided by specifying add\_library Object instead of Static.  
This will work if you specify Object not for all targets at once (imgui and imgui\_glfw), but only for the imgui\_glfw target, I don’t quite understand this either, yes imgui\_glfw links to imgui, but I specify PRIVATE for target\_link\_libraries and there should be no problems with imgui also linking to the core target, but they occur (the error example above)  
What am I doing wrong?

---

<div class="post-metadata">

### Author: ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)
#### Post date: [September 24, 2021, 3:49am UTC](https://discourse.cmake.org/t/link-two-static-libraries-to-a-shared-library/4132/2 "2021-09-24T03:49:48Z")

</div>

> [@NePutin94](#):
>
> `target_link_libraries(Core PUBLIC IMGUI IMGUI_GLFW)`

Did you mean for this to be Public? Your description suggests that this should have been Private.

---

<div class="post-metadata">

### Author: ![NePutin94](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neputin94/32/1809_2.png) [@NePutin94](https://discourse.cmake.org/u/NePutin94)
#### Post date: [September 24, 2021, 9:30am UTC](https://discourse.cmake.org/t/link-two-static-libraries-to-a-shared-library/4132/3 "2021-09-24T09:30:19Z")

</div>

There is a file in the Root that uses the Core library, if I make it PRIVATE, I will have to connect all the Core dependencies again. And this will not help, anyway, the multiple definition error for Core and IMGUI occurs during the build.  
I don’t understand where it can appear at all when linking .exe, I connect the static libraries IMGUI and IMGUI\_GLFW to Core 1 time, the Core - shared library and it is added as a target\_link in the root directory, from where the conflict between Core and IMGUI is created, if IMGUI to .the exe is not linked 2 times for sure  
In the root directory, all libraries are added via add\_subdirectory, maybe this affects something. I tried changing the structure so that imgui and imgui\_glfw lie inside the Core and are added to it via add\_subdirectory, this solves the problem, but this project structure is not correct for me
