I started toying with SDL again. Last time I was using Linux/Make and including and linking was relatively straight forward.
This time I am on a Windows 10 machine using CLion, MinGW as Toolchain and CMake. The whole thing is just a pain…
What I am trying to do is, have an “SDK” Folder in my project with all the libraries I need to link, so I can create a simple .zip/Git with the whole thing setup to start experimenting with small projects.
I managed to setup SDL2 and SDL2_image. Then I set up SDL2_ttf and SDL2_mixer in exactly the same way and it does not find the header files.
CMake on the other hand does not complain.
I can #include them directly, giving the full path, but that seems wrong.
I also seem to need to give the full path for libraries from the add-on libraries, while SDL2 is just fine with the path to a folder?
As I am new to this, I don’t even know exactly where to start debugging this. CMake right now feels super opaque in how it works to me.
Is there some kind of guide somewhere?
This is my CMakeLists.txt so far.
cmake_minimum_required(VERSION 3.24)
# Set the project Name
project(SDLBaseSetup)
#Set the C Standard to compile against
set(CMAKE_CXX_STANDARD 17)
#Set Directories for SDL2
set(SDL2_DIR ${CMAKE_SOURCE_DIR}/SDK/SDL2/cmake) #Here it will wind a config.cmake
set(SDL_LIB_DIR ${CMAKE_SOURCE_DIR}SDK/SDL2/lib) #Here are the library files located
#Set Directories for SDL2_image
set(SDL2_image_DIR ${CMAKE_SOURCE_DIR}/SDK/SDL2_image/cmake)
set(SDL2_IMAGE_LIBRARIES ${CMAKE_SOURCE_DIR}/SDK/SDL2_image/x86_64-w64-mingw32/lib/libSDL2_image.dll.a)
#Set Directories for SDL2_ttf
set(SDL2_ttf_DIR ${CMAKE_SOURCE_DIR}/SDK/SDL2_ttf/cmake)
set(SDL2_ttf_LIBRARIES ${CMAKE_SOURCE_DIR}/SDK/SDL2_ttf/x86_64-w64-mingw32/lib/libSDL2_ttf.dll.a)
#Set Directories for SDL2_mixer
set(SDL2_mixer_DIR ${CMAKE_SOURCE_DIR}/SDK/SDL2_mixer/cmake)
set(SDL2_mixer_LIBRARIES ${CMAKE_SOURCE_DIR}/SDK/SDL2_mixer/x86_64-w64-mingw32/lib/libSDL2_mixer.dll.a)
#Statically link the standard library into your executable
set(CMAKE_EXE_LINKER_FLAGS "-static")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_mixer REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_image_DIR} ${SDL2_ttf_DIR} ${SDL2_mixer_DIR})
add_executable(SDLBaseSetup main.cpp LTexture.cpp LTexture.h)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} ${SDL2_ttf_LIBRARIES} ${SDL2_mixer_LIBRARIES})
One thing I noticed. With a modern CMake, this is not required:
The find_package should set the appropriate directories automatically.
Also, maybe some of the headers are not found because target_link_libraries can’t find the library in question? I think you should use the alias the find_package creates, like in the example I gave.
Okay, I got rid of include_directories but that did not resolve the problem, sadly.
So just to clarify: set(SDL2_image_DIR ${CMAKE_SOURCE_DIR}/SDK/SDL2_image/cmake) sets a variable to this path, which contains two cmake config files.
find_package(SDL2_image REQUIRED) would look into that path and configure SDL2_image and should complain if it does not find everything?
So if this works, it should find the headers and should be able to compile, right?
target_link_libraries tells the linker to link the libraries, but compilation should already work at this point?
I’m not an expert in cmake, but that’s how I got it working. The find_package should complain to CLion’s cmake tab if it doesn’t find it. For me, the header files start working properly only after the target_link_libraries finds the correct libraries.
These two threads helped me to get the compilation working,
Thanks so much for the support!
The debug snippet was really useful.
I found that some paths were off - I guess while trying to solve the issue I broke some things…
I rebuild the whole project using fresh downloads of the libraries and a fresh CLion project, and now it seems to work.