Advertisement

Learning CMake, everything working but failing to include imgui into my project

Started by March 17, 2022 03:02 PM
1 comment, last by DaTueOwner 2 years, 8 months ago

Hello! I'm attempting to learn CMake and trying to include imgui into a particular project but failing :(

This is my project structure:

- assets
- build
- include 
- lib
	- engine
		- include
		- src
		- CMakeLists.txt (1)
	- gamecore
		- include
		- src
		- CMakeLists.txt (2)
- shaders
- src
	- main.cpp
- vendor
	- glad
	- glm
	- imgui
	- tinygltf
- CMakeLists.txt (3)

lib/engine's CMake file includes all the 3rd party libraries like GLFW, GLM, etc. Then I expose all the necessary windowing functionality to lib/gamecore and main.cpp by including files from lib/engine

Everything is working fine, but I can't seem to include imgui into lib/engine. I'm getting the following error:

\vendor\imgui\backends\imgui_impl_glfw.cpp(66,10): fatal error C1083: Cannot open include file: 'GLFW/glfw3.h': No such file or directory

Being the CMake newbie that I am, I'm pretty sure I'm doing something wrong. Would anybody have any recommendations? All I want to do is include imgui to lib/engine :(

This is lib/engine's CMake file:

project(engine)

# OpenGL
find_package( OpenGL REQUIRED )
include_directories(${OPENGL_INCLUDE_DIRS})

# GLAD
add_library(glad STATIC ../../vendor/glad/src/glad.c)
target_include_directories(glad PRIVATE ../../vendor/glad/include)

#GLFW
set(GLFW_DIR ../../vendor/glfw)
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")


## I'm trying to add it here1
set(IMGUI_DIR ../../vendor/imgui)
add_library(imgui STATIC 
    ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp 
    ${IMGUI_DIR}/imgui.cpp
)
target_include_directories(imgui PRIVATE ../../vendor/imgui)


file(GLOB SOURCES 
    src/*.cpp src/*.c 
    src/core/*.cpp src/*.c
    src/renderer/*.cpp src/*.c
    src/math/*.cpp src/*.c
    src/geometry/*.cpp src/*.c
    src/input/*.cpp src/*.c
)

# TINYGLTF
set(TINYGLTF_HEADER_ONLY ON CACHE INTERNAL "" FORCE)
set(TINYGLTF_INSTALL OFF CACHE INTERNAL "" FORCE)

add_subdirectory(${GLFW_DIR} "glfw-binaries")

add_library(${PROJECT_NAME} STATIC ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(${PROJECT_NAME} PUBLIC ${GLFW_DIR}/include)
target_include_directories(${PROJECT_NAME} PUBLIC ../../vendor/glad/include)
target_compile_definitions(${PROJECT_NAME} PRIVATE GLFW_INCLUDE_NONE)
target_include_directories(${PROJECT_NAME} PUBLIC ../../vendor/glm)
target_include_directories(${PROJECT_NAME} PUBLIC ../../vendor/tinygltf)
target_include_directories(${PROJECT_NAME} PUBLIC ${IMGUI_DIR}) # and here...

if(UNIX AND NOT APPLE)
    target_link_libraries(engine pthread)
    elseif(WIN32)
    target_link_libraries(engine glad glfw)
endif()

Oh and if you're interested in checking out the root and Gamecore Cmake files, I've uploaded them here:

Root Cmake: https://pastebin.com/BNnNrUFq
Gamecore: https://pastebin.com/VhVCsQDB

None

The problem is that the compiler cannot open glfw3.h included in an imgui file. You would need to add the glfw include path to target_include_directories. of the imgui target.
But then you also have to link against glfw when building imgui, so there wouldn't be undefined symbols.
However I would just add the required imgui cpp files to your SOURCES and compile them with your project. That way you only need to link against glfw once, in the main target.
Also why do you add the dependent libraries manually with add_library? Typically you just call add_subdirectory and link against the respective target.

This is for example how my CMake file looks like, when linking against glfw and imgui in the main project.

cmake_minimum_required(VERSION 3.9.4)
project(dooce_frontend LANGUAGES CXX C)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT dooce_frontend)
find_package(OpenGL REQUIRED COMPONENTS OpenGL)

file(GLOB_RECURSE FILES "../src/*.cpp" "../include/*.h" "extern/dearimgui/src/*.cpp")

option(SET_RELATIVE_PATH "Set a relative path for the resources for installing" OFF)

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(extern/glfw)

add_executable(dooce_frontend ${FILES} src/frontend.cpp)
target_include_directories(dooce_frontend PRIVATE extern/glfw/include)
target_include_directories(dooce_frontend PRIVATE extern/dearimgui/include)
target_include_directories(dooce_frontend PRIVATE "../include")
target_include_directories(dooce_frontend PRIVATE "include")
target_include_directories(dooce_frontend PRIVATE "extern/stbimage")
target_link_libraries(dooce_frontend glfw)
target_link_libraries (dooce_frontend ${OPENGL_LIBRARIES})
set_target_properties(dooce_frontend PROPERTIES
            CXX_STANDARD 17
            CXX_EXTENSIONS OFF
	    CXX_STANDARD_REQUIRED ON
	     )

if(SET_RELATIVE_PATH)
target_compile_definitions(dooce_frontend PUBLIC RESOURCE_DIR=\"pieces\") 
else()
target_compile_definitions(dooce_frontend PUBLIC RESOURCE_DIR=\"${CMAKE_SOURCE_DIR}/frontend/pieces\") 
endif()

install(TARGETS dooce_frontend DESTINATION dooce_application)
install(DIRECTORY pieces/ DESTINATION dooce_application/pieces)

This topic is closed to new replies.

Advertisement