0

Currently I'm trying to add SDL3 and ImGui to my 2D game engine's config.cmake file.

I would like to use that library as below from external project.

find_package(MyEngine REQUIRED)
add_executable(MyGame game.cpp)
target_link_librariess(MyGame PRIVATE MyEngine)
 
// MyEngine will already contain SDL3 and ImGui libraries
// so you don't have to include and compile them by yourselves

Following CMakeLists.txt is a file I wrote to install myEngine without SDL3 and ImGui.

cmake_minimum_required(VERSION 3.30)
project(MyEngine
        DESCRIPTION "Simple 2D Game Engine"
        VERSION 0.0.1)

include(GNUInstallDirs)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INSTALL_PREFIX "MyEngine-${CMAKE_PROJECT_VERSION}")

add_library(Engine STATIC src/Engine.cpp)
add_library(Player STATIC src/Player.cpp)
add_library(World STATIC src/World.cpp)

# Set include directories for all libraries
foreach(lib Engine Player World)
    target_include_directories(${lib} PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
    )
endforeach()

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/MyEngine DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(TARGETS Engine Player World
    EXPORT MotionTargets
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT MyEngineTargets
    FILE MotionTargets.cmake
    NAMESPACE MyEngine::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/MyEngine
)

include(CMakePackageConfigHelpers)

configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/MyEngine-config.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/MyEngineConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/MyEngine
)

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/MyEngineConfig.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/MyEngine
)

So MyEngine uses SDL3 and ImGui and I want to include them into my project on order to simplify it's integration but I don't know how to include SDL3

How can I include SDL3, SDLRenderer3 and ImGui library with the above CMakeLists.txt?

1
  • look into how sdl-ttf packages and installs harfbuzz and maybe do the same yourself. Commented Jun 29 at 6:30

1 Answer 1

0

regarding your question, I have provided a simple usage example here.

$ tree . -L 2
.
├── CMakeLists.txt
├── deps
│   └── SDL
└── main.cc

As shown above, this is a very simple file organization: the source code of the SDL3 library you need is stored in deps/SDL. Below is a very simple CMakeLists.txt that I wrote.

cmake_minimum_required(VERSION 3.10.0)
project(test-sdl VERSION 0.1.0 LANGUAGES C CXX)

# IF YOU DOWNLOAD LIBSDL3 OR YOU INSTALL FROM SOURCE CODE
# find_package(SDL3 REQUIRED)

add_subdirectory(deps/SDL EXCLUDE_FROM_ALL)

add_executable(test-sdl main.cc)

target_link_libraries(test-sdl PRIVATE 
    SDL3::SDL3
)

Here, I assume you need to compile from source code. If you have obtained libsdl3-dev directly from a package management system, please use the commented code above. This is also a simple example from the official documentation: SDL INTRO

As for the ImGUI library, the official recommendation is not to directly produce dynamic or static libraries:ImGUI

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.