SDL static compillation

Hello! I want to compile my project with SDL2, SDL2_image, SDL2_ttf, SDL2_mixer, but I cant find how to create cmake file for it. Ive been searching for it for a really long time. Please help me!

create cmake file for it

What is “it” in here? :slight_smile:

You need help with creating an entire project file from scratch? Then I’d recommend starting with some tutorials: https://cmake.org/cmake/help/latest/guide/tutorial/index.html
If you already have at least a draft of your project file, then share it and describe the problems you have.

Or do you need help with building SDL from sources? In that case, it already has CMakeLists.txt, so you shouldn’t need to do anything special for that. Do you have some specific issues with it?

I set the direct path to the folders, but at the result the binary file asks the libs from the /usr/lib. Idk why…

How do you set that path and what folders do you mean? It would be useful to see at least this particular fragment from your project file.

http://boyaroslav.pythonanywhere.com/gitreps/ultra_hl_cpp/CMakeLists.txt/look?branch=master

please look. I paste an sdl folder with .so libs to the project and after that I try to cmake it. Sorry for 3 days gap

i have .so in the sdl folder because I dont know how to staticly compile it. So I try to keep libs in the project

I see, so you don’t use CMake configs for discovering SDL. In that case you first need to “find” the SDL binaries with find_library(), and only then you will be able to link to it with target_link_libraries(). And you will most likely also need to add SDL headers to your project’s include directories. So an example would look like this:

set(SDL_PREFIX "/path/to/sdl/installation") # can be set via CLI with -D
find_library(sdl SDL2 PATHS "${SDL_PREFIX}/lib") # SDL2_image SDL2_ttf SDL2_mixer ...
target_include_directories(${PROJECT_NAME}
    PRIVATE
        "${SDL_PREFIX}/include"
)
target_link_libraries(${PROJECT_NAME}
    PRIVATE
        sdl # SDL2_image SDL2_ttf SDL2_mixer ...
)

But I would recommend you to use CMake configs provided by SDL - in that case you’ll only need to use find_package(), for example:

# it is assumed that path to SDL installation is added to CMAKE_PREFIX_PATH
find_package(SDL2 CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME}
    PRIVATE
        SDL2::SDL2
        SDL2::SDL2main
        #SDL2::SDL2-static
        #...
)

I dont know how to staticly compile it

How did you try to compile it? Not setting BUILD_SHARED_LIBS or explicitly setting it to 0 should be enough. Or are you not using CMake to build SDL?

I have libSDL2-2.0.so libSDL2_mixer.so libSDL2_ttf.so libSDL2_image.so libSDL2_net.so in the sdl subfolder. I want my programm to get the dependencies from this folder. Not from /usr/lib. How can I generate Makefile by cmake that will compile it for me?

structure of the project:
project:
sdl/
main.cpp
CMakeLists.txt

set(SDL_PREFIX "/path/to/sdl/installation")

Quite obviously, you need to replace that path with yours.