Last active
January 18, 2024 05:52
-
-
Save bolducke/98575d9a544654b5c659e425e20d3ba4 to your computer and use it in GitHub Desktop.
Modern CMake (3.11+) Example with Imgui+Glad+SDL2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Directory Structure | |
| ####### | |
| # build/ | |
| # ... | |
| # extern/ | |
| # glad/ | |
| # ... | |
| # imgui/ | |
| # ... | |
| # src/ | |
| # *.cpp | |
| # *.h | |
| cmake_minimum_required(VERSION 3.11) | |
| message(${CMAKE_CURRENT_SOURCE_DIR}) | |
| project(NAME VERSION 1.0.0 DESCRIPTION "" LANGUAGES C CXX) | |
| set(PROJECT_NAME NAME) | |
| set(CMAKE_CXX_STANDARD 11) | |
| set(CMAKE_CXX_EXTENSIONS OFF) | |
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | |
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}>) | |
| # ============================================================================ | |
| # PACKAGE INSTALLATION | |
| # ============================================================================ | |
| ## GLAD | |
| ######### | |
| set(GLAD_PATH ${CMAKE_SOURCE_DIR}/extern/glad) | |
| add_library(glad STATIC | |
| ${GLAD_PATH}/src/glad.c | |
| ${GLAD_PATH}/include/glad/glad.h | |
| ) | |
| target_include_directories(glad | |
| PUBLIC | |
| ${GLAD_PATH}/include/ | |
| ) | |
| ## SDL2 | |
| ########## | |
| find_package(SDL2 REQUIRED) | |
| include_directories(${SDL2_INCLUDE_DIRS}) | |
| # ## IMGUI | |
| # ########## | |
| set(IMGUI_PATH ${CMAKE_SOURCE_DIR}/extern/imgui) | |
| # im-core | |
| add_library(im-core | |
| ${IMGUI_PATH}/imgui.cpp | |
| ${IMGUI_PATH}/imgui_demo.cpp | |
| ${IMGUI_PATH}/imgui_draw.cpp | |
| ${IMGUI_PATH}/imgui_widgets.cpp | |
| ${IMGUI_PATH}/imgui_tables.cpp | |
| ${IMGUI_PATH}/imgui.h | |
| ${IMGUI_PATH}/imgui_internal.h | |
| ) | |
| target_include_directories(im-core | |
| PUBLIC | |
| $<BUILD_INTERFACE:${IMGUI_PATH}> | |
| $<INSTALL_INTERFACE:imgui.h> | |
| $<INSTALL_INTERFACE:imgui_internal.h> | |
| ) | |
| # im-sdl2 | |
| add_library(im-sdl2 | |
| ${IMGUI_PATH}/backends/imgui_impl_sdl2.cpp | |
| ${IMGUI_PATH}/backends/imgui_impl_sdl2.h | |
| ) | |
| target_include_directories(im-sdl2 | |
| PUBLIC | |
| $<BUILD_INTERFACE:${IMGUI_PATH}/backends> | |
| $<INSTALL_INTERFACE:imgui_impl_sdl2.h> | |
| ) | |
| target_link_libraries(im-sdl2 | |
| im-core | |
| SDL2::SDL2 | |
| ) | |
| # im-opengl | |
| add_library(im-opengl3 | |
| ${IMGUI_PATH}/backends/imgui_impl_opengl3.cpp | |
| ${IMGUI_PATH}/backends/imgui_impl_opengl3.h | |
| ) | |
| target_include_directories(im-opengl3 | |
| PUBLIC | |
| $<BUILD_INTERFACE:${IMGUI_PATH}/backends> | |
| $<INSTALL_INTERFACE:imgui_impl_opengl3.h> | |
| ) | |
| target_link_libraries(im-opengl3 | |
| glad | |
| im-core | |
| ) | |
| # ============================================================================ | |
| # SRC | |
| # ============================================================================ | |
| add_executable(${PROJECT_NAME} src/main.cpp) | |
| target_sources(${PROJECT_NAME} | |
| PUBLIC | |
| ${CMAKE_CURRENT_LIST_DIR}/src/main.cpp | |
| # Add more of your source file here | |
| ) | |
| target_include_directories(${PROJECT_NAME} | |
| PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src) | |
| target_link_libraries(${PROJECT_NAME} | |
| glad | |
| im-core | |
| im-sdl2 | |
| im-opengl3 | |
| SDL2::SDL2 | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is simply a reference for my future self, but I would welcome changes if they are pertinent.