The structure of my project:
+--ROOT
-CmakeLists.txt
+--imgui
-CmakeLists.txt
+--core
-CmakeLists.txt
+--imgui_glfw
-CmakeLists.txt
Root CmakeLists.txt adds imgui, imgui_glfw, and core as subdirectory:
...
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:
...
add_library(Core SHARED ${CORE_SRC} ${HEADER_SOURCE})
target_link_libraries(Core PUBLIC IMGUI IMGUI_GLFW)
imgui CmakeLists:
...
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:
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:
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?