mosra / magnum-integration

Integration libraries for the Magnum C++11 graphics engine
https://magnum.graphics/
Other
97 stars 44 forks source link

How to use ImGui integration as subdirectory #75

Closed photex closed 3 years ago

photex commented 3 years ago

Howdy!

I can compile but not link the ImGui example currently (Windows, VS2019). I get linker errors regarding the event handlers in Context:

main.cpp.obj : error LNK2019: unresolved external symbol "public: void __cdecl Magnum::ImGuiIntegration::Context::updateApplicationCursor<class Kpns::UI::MainWindow>(class Kpns::UI::MainWindow &)" (??$updateApplicationCursor@VMainWindow@UI@Kpns@@@Context@ImGuiIntegration@Magnum@@QEAAXAEAVMainWindow@UI@Kpns@@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::drawEvent(void)" (?drawEvent@MainWindow@UI@Kpns@@EEAAXXZ)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleKeyPressEvent<class Magnum::Platform::GlfwApplication::KeyEvent>(class Magnum::Platform::GlfwApplication::KeyEvent &)" (??$handleKeyPressEvent@VKeyEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVKeyEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::keyPressEvent(class Magnum::Platform::GlfwApplication::KeyEvent &)" (?keyPressEvent@MainWindow@UI@Kpns@@EEAAXAEAVKeyEvent@GlfwApplication@Platform@Magnum@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleKeyReleaseEvent<class Magnum::Platform::GlfwApplication::KeyEvent>(class Magnum::Platform::GlfwApplication::KeyEvent &)" (??$handleKeyReleaseEvent@VKeyEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVKeyEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::keyReleaseEvent(class Magnum::Platform::GlfwApplication::KeyEvent &)" (?keyReleaseEvent@MainWindow@UI@Kpns@@EEAAXAEAVKeyEvent@GlfwApplication@Platform@Magnum@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleMousePressEvent<class Magnum::Platform::GlfwApplication::MouseEvent>(class Magnum::Platform::GlfwApplication::MouseEvent &)" (??$handleMousePressEvent@VMouseEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVMouseEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::mousePressEvent(class Magnum::Platform::GlfwApplication::MouseEvent &)" (?mousePressEvent@MainWindow@UI@Kpns@@EEAAXAEAVMouseEvent@GlfwApplication@Platform@Magnum@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleMouseReleaseEvent<class Magnum::Platform::GlfwApplication::MouseEvent>(class Magnum::Platform::GlfwApplication::MouseEvent &)" (??$handleMouseReleaseEvent@VMouseEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVMouseEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::mouseReleaseEvent(class Magnum::Platform::GlfwApplication::MouseEvent &)" (?mouseReleaseEvent@MainWindow@UI@Kpns@@EEAAXAEAVMouseEvent@GlfwApplication@Platform@Magnum@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleMouseMoveEvent<class Magnum::Platform::GlfwApplication::MouseMoveEvent>(class Magnum::Platform::GlfwApplication::MouseMoveEvent &)" (??$handleMouseMoveEvent@VMouseMoveEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVMouseMoveEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::mouseMoveEvent(class Magnum::Platform::GlfwApplication::MouseMoveEvent &)" (?mouseMoveEvent@MainWindow@UI@Kpns@@EEAAXAEAVMouseMoveEvent@GlfwApplication@Platform@Magnum@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleMouseScrollEvent<class Magnum::Platform::GlfwApplication::MouseScrollEvent>(class Magnum::Platform::GlfwApplication::MouseScrollEvent &)" (??$handleMouseScrollEvent@VMouseScrollEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVMouseScrollEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::mouseScrollEvent(class Magnum::Platform::GlfwApplication::MouseScrollEvent &)" (?mouseScrollEvent@MainWindow@UI@Kpns@@EEAAXAEAVMouseScrollEvent@GlfwApplication@Platform@Magnum@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Magnum::ImGuiIntegration::Context::handleTextInputEvent<class Magnum::Platform::GlfwApplication::TextInputEvent>(class Magnum::Platform::GlfwApplication::TextInputEvent &)" (??$handleTextInputEvent@VTextInputEvent@GlfwApplication@Platform@Magnum@@@Context@ImGuiIntegration@Magnum@@QEAA_NAEAVTextInputEvent@GlfwApplication@Platform@2@@Z) referenced in function "private: virtual void __cdecl Kpns::UI::MainWindow::textInputEvent(class Magnum::Platform::GlfwApplication::TextInputEvent &)" (?textInputEvent@MainWindow@UI@Kpns@@EEAAXAEAVTextInputEvent@GlfwApplication@Platform@Magnum@@@Z)

My relevant CMake script that sets up Magnum:

##############################################################################
# Magnum (and it's associated subprojects)
#
set(BUILD_STATIC ON CACHE BOOL "" FORCE)

set(WITH_TESTSUITE OFF CACHE BOOL "" FORCE)
add_subdirectory(source/corrade EXCLUDE_FROM_ALL)

set(WITH_AUDIO OFF CACHE BOOL "" FORCE)
set(WITH_GLFWAPPLICATION ON CACHE BOOL "" FORCE)
add_subdirectory(source/magnum EXCLUDE_FROM_ALL)

set(WITH_EIGEN ON CACHE BOOL "" FORCE)
set(WITH_IMGUI ON CACHE BOOL "" FORCE)
set(IMGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source/imgui)
add_subdirectory(source/magnum-integration EXCLUDE_FROM_ALL)

set(WITH_PNGIMAGECONVERTER ON CACHE BOOL "" FORCE)
set(WITH_PNGIMPORTER ON CACHE BOOL "" FORCE)
add_subdirectory(source/magnum-plugins EXCLUDE_FROM_ALL)

And because I set this all up from a subdirectory under the repo root I provide a global interface library to avoid having to repeat myself for anything that uses Magnum:

add_library(kpns_graphics INTERFACE)
target_link_libraries(kpns_graphics INTERFACE
  Corrade::Main
  Magnum::GlfwApplication
  Magnum::GL
  Magnum::Magnum
  Magnum::Shaders
  Magnum::SceneGraph
  Magnum::DebugTools
  Magnum::Primitives
  Magnum::Text
  Magnum::TextureTools
  Magnum::Trade
  MagnumIntegration::ImGui
  MagnumIntegration::Eigen
  )

My application adds kpns_graphics to it's PRIVATE target_link_libraries.

So, everything compiles fine, CLion resolves all symbols etc. But when linking it fails and I'm not sure why. I'd appreciate any helpful advice.

Thanks!

photex commented 3 years ago

I've just tested a fresh build and I get this error even when building using shared libraries.

photex commented 3 years ago

Welp, I realized after some head scratching that I was including Context.h instead of Context.hpp. After using hpp my linker problems appear to be resolved... but none of the events appear to get handled or drawn correctly.

mosra commented 3 years ago

Hi!

Glad you at least partially solved this already -- I wouldn't be able to discover that without seeing the source. Can you elaborate on how the events are incorrect? Are they not handled at all? Or are they happening at wrong positions? etc. Including relevant parts of the source code would help also, thanks! :)

photex commented 3 years ago

😆 was just about to give an update.

I verified that the event callbacks and imgui context methods were all correctly getting called. I realized there must be another issue. I had missed the call to redraw(). 🤦‍♀️

Onwards!

mosra commented 3 years ago

Haha :laughing:

Good to see issues solving themselves if I take ages to reply.

mosra commented 3 years ago

I just updated the docs to highlight those two common pitfalls a bit better: 1eae5514bb66530b58898f5858f6a8d231279fa5 Not in the online docs yet, will do so in the next upload batch.