Home > Back-end >  Make a library with cmake that use the SDL
Make a library with cmake that use the SDL

Time:12-02

I am trying to create a library on the top of SDL2. For the compilation I'm using cmake. First I had this CMakeLists.txt for the library :

cmake_minimum_required(VERSION 3.10)

project(SquareEngine)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)

include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${SDL2_IMAGE_INCLUDE_DIRS})

set (SRCS   Main.cpp
        Rectangle.cpp
        Scene.cpp
        SquareEngine.cpp)

set (HEADERS    Rectangle.hpp
        Scene.hpp
        SquareEngine.hpp
        utils/Color.hpp
        utils/Vector.hpp)

add_library(SquareEngine ${SRCS} ${HEADERS} ${SDL} ${SDL_SRC})
target_link_libraries(SquareEngine ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})

To include the SDL2 and SDL2 image I follow this instructions.

When I try to build it's work perfectly.

Now I try to use my library in a project. I create my own project and I link my librairy that is located in a subfolder /SquareEngine in my project. For the project I use this CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)

project(Demo)


set (SRCS Main.cpp)
add_subdirectory(SquareEngine)
add_executable(Demo ${SRCS})
target_link_libraries(Demo PUBLIC SquareEngine)
target_include_directories(Demo PUBLIC SquareEngine)

When I try to build the project I got an error like this : Cannot open include file: 'SDL.h': No such file or directory

I try to figure out for long time and I don't have any clue why.

Maybe it will be better to use the SDL in static rather than shared ?

CodePudding user response:

I think you need to add the SDL include dirs to the SquareEngine library:

target_include_directories(SquareEngine PUBLIC ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})
  • Related