0

I'm trying to compile a c++ program with the SDL package using cmake but I have issues finding the SDL.h file when I compile it with the make command. Currently the CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)

project(TestProject VERsion 1.0 DESCRIPTION "Game project" LANGUAGES CXX)

add_compile_options(-Wall -Wextra -pedantic -Werror)

add_library(Gamelib ${PROJECT_SOURCE_DIR}/Source/Game/game.cc)

target_include_directories(Gamelib PUBLIC ${PROJECT_SOURCE_DIR}/Source/include 
${PROJECT_SOURCE_DIR}/SDL/include)

add_executable(main ${PROJECT_SOURCE_DIR}/Source/main.cc)
target_link_libraries(main Gamelib)


The project structure looks like this:

|--project
   | 
   +-- CMakeLists.txt
   |
   +-- Source
   |    |
   |    +--Game
   |    |   |
   |    |   +-- game.cc
   |    |
   |    +--include
   |    |   |
   |    |    +--game.h
   |    |
   |    +-- main.cc
   |
   |
   +--SDL
      |
      +--include

I also tried with using the find_package command but then I ended up with a linker error instead. Does anybody have an idea of how I can solve this?

user202542
  • 211
  • 1
  • 8
  • The integration of SDL2 with CMake is a mess, they only tackled the topic recently. So the answer will heavily depend on what version of SDL2 you want to use. You will need to `find_package(SDL2 REQUIRED)` and then either use variables or targets defined by the package. Details depend on version alas. — If your version has proper targets in place, `target_link_libraries(Gamelib PUBLIC SDL2::SDL2)` after the find_package should be all you need. – spectras May 22 '22 at 13:27
  • Thank you! Why is the find package required? Is it the version of cmake it depends on? Do you know how I find the details? – user202542 May 22 '22 at 14:48
  • 2
    "Do you know how I find the details?" - There are plenty questions on Stack Overflow about using SDL with CMake. You can easily [google them](https://www.google.com/search?q=cmake+sdl+site:stackoverflow.com). In the current state your question lacks many details, like **exact error message**, **exact attempt** of using `find_package` (code and the error message it causes), etc. So we cannot help you aside from reiterating information from already existing questions. – Tsyvarev May 22 '22 at 14:51
  • Does this answer your question? [Linking SDL2 with CMake](https://stackoverflow.com/questions/62644557/linking-sdl2-with-cmake) – Alex Reinking May 22 '22 at 15:57

0 Answers0