# SDL static compillation

**URL:** <https://discourse.cmake.org/t/sdl-static-compillation/11021>\
**Category:** Code\
**Tags:** os:linux, gen:makefiles\
**Created:** [June 14, 2024, 2:15pm UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021 "2024-06-14T14:15:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![boyaroslav](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@boyaroslav](https://discourse.cmake.org/u/boyaroslav)\
**Post date:** [June 14, 2024, 2:15pm UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/1 "2024-06-14T14:15:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 14, 2024, 2:25pm UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/2 "2024-06-14T14:25:15Z")

</div>

> create cmake file for it

What is “it” in here? 🙂

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](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](https://github.com/libsdl-org/SDL/blob/main/CMakeLists.txt), so you shouldn’t need to do anything special for that. Do you have some specific issues with it?

---

<div class="post-metadata">

**Author:** ![boyaroslav](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@boyaroslav](https://discourse.cmake.org/u/boyaroslav)\
**Post date:** [June 14, 2024, 2:29pm UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/3 "2024-06-14T14:29:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 14, 2024, 2:52pm UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/4 "2024-06-14T14:52:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boyaroslav](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@boyaroslav](https://discourse.cmake.org/u/boyaroslav)\
**Post date:** [June 17, 2024, 9:18am UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/5 "2024-06-17T09:18:01Z")

</div>

[http://boyaroslav.pythonanywhere.com/gitreps/ultra\_hl\_cpp/CMakeLists.txt/look?branch=master](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

---

<div class="post-metadata">

**Author:** ![boyaroslav](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@boyaroslav](https://discourse.cmake.org/u/boyaroslav)\
**Post date:** [June 17, 2024, 9:21am UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/6 "2024-06-17T09:21:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 17, 2024, 9:59am UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/7 "2024-06-17T09:59:19Z")

</div>

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()](https://cmake.org/cmake/help/latest/command/find_library.html), 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:

```auto
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()](https://cmake.org/cmake/help/latest/command/find_package.html), for example:

```auto
# 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?

---

<div class="post-metadata">

**Author:** ![boyaroslav](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@boyaroslav](https://discourse.cmake.org/u/boyaroslav)\
**Post date:** [June 17, 2024, 10:31am UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/8 "2024-06-17T10:31:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 17, 2024, 11:44am UTC](https://discourse.cmake.org/t/sdl-static-compillation/11021/9 "2024-06-17T11:44:45Z")

</div>

> ```auto
> set(SDL_PREFIX "/path/to/sdl/installation")
> 
> ```

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