Trouble Including ImGui in C++ Project with CMake

29 Views Asked by At

i'm facing some challenges while trying to integrate ImGui into my C++ project using CMake and VS Code. As a newcomer to CMake, I have limited knowledge and have been following this blog post https://www.ics.com/blog/find-and-link-libraries-cmake for guidance. Despite successfully configuring CMake without errors, I'm encountering difficulties with missing header files during compilation.

Project Structure:

Project/
├── CMakeLists.txt (Main project)
├── src/
│   └── main.cpp
└── imgui/
    ├── CMakeLists.txt (Library project)
    ├── imgui.cpp
    ├── imgui.h
    └── (Other library files)

Project Structure picture

CMake Configuration: Here's an overview of the CMakeLists.txt files in my main project and library project:

CMakeLists.txt (Main Project):

cmake_minimum_required(VERSION 3.17)
project(AutoBackupScript VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)

# adding sourcefiles 
set(SOURCES
    src/main.cpp

)

include_directories(headers)

include_directories(${CMAKE_SOURCE_DIR}/imgui)


add_executable(AutoBackupScript ${SOURCES})

target_link_libraries(AutoBackupScript PRIVATE imgui)

# looking for Sfml
set(SFML_DIR "c:/SFML/lib/cmake/SFML")  # Path to SFML CMake-Modul
find_package(SFML COMPONENTS system window graphics network audio REQUIRED)
if (SFML_FOUND)
    target_link_libraries(AutoBackupScript PRIVATE sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif()

# copies DLLs in the Build-Dir
if(WIN32)
    file(GLOB BINARY_DEP_DLLS "c:/SFML/bin/*.dll")
    file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
    file(GLOB MINGW_DEP_DLLS "C:/mingw64/bin/*.dll")
    file(COPY ${MINGW_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
endif()

CMakeLists.txt (Library Project):

cmake_minimum_required(VERSION 3.17)


set(IMGUI_SOURCES
    imgui.cpp
    imgui.h
)

# creating ImGui-Lib
add_library(imgui ${IMGUI_SOURCES})

target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

Error Message:

[main] Building folder: AutoBackupScript 
[build] Starting build
[proc] Executing command: chcp
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript/build --config Debug --target all -j 6 --
[build] [ 50%] Building CXX object CMakeFiles/AutoBackupScript.dir/src/main.cpp.obj
[build] C:\Users\luka-\OneDrive\Desktop\AutoBackupScript\src\main.cpp:13:10: fatal error: imgui/imgui.h: No such file or directory
[build]    13 | #include "imgui/imgui.h"
[build]       |          ^~~~~~~~~~~~~~~
[build] compilation terminated.
[build] mingw32-make[2]: *** [CMakeFiles\AutoBackupScript.dir\build.make:76: CMakeFiles/AutoBackupScript.dir/src/main.cpp.obj] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/AutoBackupScript.dir/all] Error 2
[build] mingw32-make: *** [Makefile:90: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript/build --config Debug --target all -j 6 -- exited with code: 2
[driver] Build completed: 00:00:00.890
[build] Build finished with exit code 2

I've been following the steps outlined in the blog post and have set up ImGui as an external library. However, despite my best efforts, I'm consistently encountering errors related to missing header files during compilation. It's worth noting that I've received no CMake errors throughout this process.

If anyone has experience integrating ImGui into a C++ project using CMake and VS Code, or if you've encountered similar issues before, I would greatly appreciate any insights or suggestions you may have.

Thank you in advance for your assistance!

I've attempted to follow the steps outlined in the blog post, setting up ImGui as source-only libraries both as part of the main application and as a separate library. However, despite my efforts, I haven't been able to achieve the expected results, and I'm unable to find a solution. I haven't encountered any CMake errors during this process.

0

There are 0 best solutions below