c++cmakesdlimguisdl-3

Imgui can not find SDL3/SDL.h file


I am trying to learn imgui by first create a project using its library. But, first I wanted to run one of the examples found in the github repository. I am not using an IDE, only my mac terminal. Therefore, I am using cmake to build the project. Here is my project directory structure:

imgui_project
    |____build
    |____CMakeLists.txt
    |____src
    |     |__CMakeLists.txt
    |     |__main.cpp
    |____Vendors
          |___CMakeLists.txt
          |___Imgui
          |     |____CMakeLists.txt
          |___sdl
                |____CMakeLists.txt

The content in the files:

imgui_project/CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
project(imgui_app)
add_subdirectory(vendors)
add_subdirectory(src)


src/CMakeLists.txt

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE SDL3 imgui)


src/main.cpp

{the entire content of example_sdl3_renderer3 main.cpp file from the imgui github repository.}


Vendors/CMakeLists.txt

add_subdirectory(sdl)
add_subdirectory(imgui)


Vendors/imgui/CMakeLists.txt

include(FetchContent)

FetchContent_Declare(
    imgui
    GIT_REPOSITORY https://github.com/ocornut/imgui.git
    GIT_TAG docking
)
FetchContent_MakeAvailable(imgui)
add_library(imgui)
target_include_directories(
    imgui
    PUBLIC 
    ${imgui_SOURCE_DIR}
    ${imgui_SOURCE_DIR}/backends
)
target_sources(
    imgui
    PUBLIC 
    ${imgui_SOURCE_DIR}/imgui.cpp
    ${imgui_SOURCE_DIR}/imgui_draw.cpp
    ${imgui_SOURCE_DIR}/imgui_demo.cpp
    ${imgui_SOURCE_DIR}/imgui_tables.cpp
    ${imgui_SOURCE_DIR}/imgui_widgets.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_sdlrenderer3.cpp
)
target_link_directories(imgui PUBLIC SDL3)


Vendors/sdl/CMakeLists.txt

include(FetchContent)
FetchContent_Declare(
    SDL3
    GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
    GIT_TAG release-3.2.20
)
FetchContent_MakeAvailable(SDL3)

I build the project in the build directory using the command: cmake .. -G "Unix Makefiles"
When I run make, the error I am getting:

.../imgui_project/build/_deps/imgui-src/backends/imgui_impl_sdl3.cpp:83:10: fatal error: 'SDL3/SDl.h' file not found
83 | #include <SDL3/SDL.h>

In the SDL3 github repository, 3.2.20 release, in the include/SDl3/ directory, SDL.h is there. I thought having the FetchContent_MakeAvailable(SDL3) would make the library available in the build after fetching it. So, I am not sure why it is not able to find the SDL.h file. That is the only error I am currently getting.


Solution

  • The build system generates the SDL3 library in the build folder, but imgui was not searching in the correct directory due to the command issue. target_link_directories(imgui PUBLIC SDL3) in the vendors/imgui/CMakeLists.txt file, on the last line, needs to be target_link_libraries(imgui PUBLIC SDL3::SDL3).