mosra / magnum-integration

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

How to use ImGui integration when adding this project as a cmake subdirectory? #43

Closed lasagnaphil closed 5 years ago

williamjcm commented 5 years ago

Something like the below snippet should work:

set(WITH_IMGUI ON)
set(IMGUI_DIR "/path/to/imgui/sources")
add_subdirectory(magnum-integration)

That's how I do it on my end.

lasagnaphil commented 5 years ago

Sorry for the blank description, I've accidentally pressed Enter before typing a thing and I couldn't roll back...

I am trying to add this library to my project using git submodules. However, using add_subdirectory() doesn't seem to be as straightforward than I've thought. (I've set IMGUI_DIR to point at the imgui directory before calling add_subdirectory().)

My current CMakeLists.txt for the executable (which was the best I could do):

file(GLOB DEMO_SOURCES *.cpp)
file(GLOB DEMO_HEADERS *.h)

set(PROJECT_DEMO_NAME ${PROJECT_NAME}_demo)

set(WITH_IMGUI ON)
set(IMGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps/imgui)

set(MAGNUM_INTEGRATION_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps/magnum-integration)
add_subdirectory(${MAGNUM_INTEGRATION_DIR})

find_package(Magnum REQUIRED Sdl2Application)

set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON)

add_executable(${PROJECT_DEMO_NAME} ${DEMO_SOURCES} ${DEMO_HEADERS})
target_include_directories(${PROJECT_DEMO_NAME} PRIVATE ${PROJECT_NAME} ${IMGUI_DIR} ${MAGNUM_INTEGRATION_DIR}/src)
target_link_libraries(${PROJECT_DEMO_NAME} PRIVATE ${PROJECT_NAME}
        Magnum::Magnum
        Magnum::Application
        Magnum::GL
        ImGui)

But after this, I get a compilation error:

**project name**/demo/deps/magnum-integration/src/Magnum/ImGuiIntegration/visibility.h:33:10: fatal error: Magnum/ImGuiIntegration/configure.h: No such file or directory
    #include "Magnum/ImGuiIntegration/configure.h"

Any ideas on why this is happening? (Are there other configuration steps that I need to do before linking the library?)

mosra commented 5 years ago

Hi! The problem is, as far as I can see, with using target_include_directories(${IMGUI_DIR} ${MAGNUM_INTEGRATION_DIR}/src). That's the "old-style" CMake and these two are not enough to cover all include paths when using the CMake subproject approach -- in particular, the configure.h file is generated and located somewhere inside the build directory. If you, instead, link to MagnumIntegration::ImGui similarly as you already do for other Magnum libs, then all should work:

target_include_directories(${PROJECT_DEMO_NAME} PRIVATE ${PROJECT_NAME})
target_link_libraries(${PROJECT_DEMO_NAME} PRIVATE ${PROJECT_NAME}
        Magnum::Magnum
        Magnum::Application
        Magnum::GL
        MagnumIntegration::ImGui)

I'm also not sure if you really need the target_include_directories(${PROJECT_DEMO_NAME} PRIVATE ${PROJECT_NAME}) part because if ${PROJECT_NAME} is a target, then its include directories are already handled by target_link_libraries(... ${PROJECT_NAME}) -- but also I don't know how your project is structured, so I might be wrong ;)

(If I can suggest anything -- regarding CMake coding style, I'm personally always trying to avoid variables and use concrete target names where possible. There are old CMake tutorials on the internet that tend to use variables excessively (especially the ${PROJECT_NAME}), but that's not considered a good practice with modern CMake anymore. So, let's say your project would be named FooBar, then instead of target_link_libraries(${PROJECT_DEMO_NAME} PRIVATE ${PROJECT_NAME}) I would write just target_link_libraries(FooBar_demo PRIVATE FooBar). Similar goes for source file listing -- the preferred way is to list them all explicitly instead of using file(GLOB).)

lasagnaphil commented 5 years ago

Thanks, using your suggestion it worked perfectly! Sorry for asking a trivial question. I'm still a novice in CMake, and sometimes I get confused with things a lot...

PS: Right now, I have two targets in my project. ${PROJECT_NAME} is the actual library (which will only do numerical computation and will not include Magnum), and ${PROJECT_DEMO_NAME} is the demo which will use the library and do some visualization. I agree that I shouldn't use variables excessively... (although this was because I was reusing a template project I've made before)

mosra commented 5 years ago

No problem with asking trivial questions, happy to help -- also, nobody ever became an expert without being a novice first ;)