Install sdl for linux

Setting up SDL 2 on Linux

Since there are so many flavors of Linux these tutorials might not work on your Linux set up. If you’ve tried everything and are still having problems, contact me and I’ll try to add on a distro specific fix.

In the age of packaging managers, a lot of the work is done for you when installing libraries. You’ll need root privileges to install packages so make sure to use «su» or «sudo» if you’re not logged in as root.

Make sure you have updated to the latest version of your distro because your older package manager may not support the latest version of SDL 2.

1) For those of you who have Advanced Packaging Tool available (ie Ubuntu and Debian) you’ll want to search the apt-get cache to find the current SDL 2 version to install. You can search the apt-get available packages using the command:

You’ll want to download the development version of SDL 2. As of the last update of this tutorial, the development package of SDL 2 is libsdl2-dev. You can install this package using the command

apt-get install libsdl2-dev

2) If you use the Yellow dog Updater, Modified (used in Fedora and CentOS) you can enter the command:

To search for the SDL 2 developers package. As of the last update of this tutorial, the developer package for SDL 2 is SDL2-devel. You can install this package using the command:

3) If somehow you don’t have a package manager, you can install from the source the classic Unix way. Download the latest source from the SDL GitHub.

sdl2 source download page

Extract the archive and cd to the folder that got extracted. Configure the installation using

Compile the source using the make command

Finally, install the package using the make command

Note: If you do a manual install you may have to specify where the headers and library files are for your compiler/IDE.

4) Now that you’ve installed the development libraries, it’s time to start up your IDE/compiler.

Copyright Lazy Foo’ Productions 2004-2023

Источник

Install sdl for linux

The libsdl direct media library is open source and cross-platform. It helps that it’s also part of Linux already. You can see this if you open a terminal and do a

apt-cache search libsdl2 | grep dev

This will show you what libraries are available

david@david-VM-PC9:~$ apt-cache search libsdl2 | grep dev libsdl2-dev - Simple DirectMedia Layer development files libsdl2-gfx-dev - development files for SDL2_gfx libsdl2-image-dev - Image loading library for Simple DirectMedia Layer 2, development files libsdl2-mixer-dev - Mixer library for Simple DirectMedia Layer 2, development files libsdl2-net-dev - Network library for Simple DirectMedia Layer 2, development files libsdl2-ttf-dev - TrueType Font library for Simple DirectMedia Layer 2, development files

Although SDL is available, the dev files aren’t installed by default and we want all the headers for C/C++ programs to use them.

Читайте также:  Linux размер дискового пространства

We need to install the devs for the libraries we’ll need which are libsdl2-dev, libsdl2-image-dev (you do want to display graphics don’t you?), libsdl2-mixer-dev (for sounds) and if you want text you’ll need libsdl2-ttf-dev.

So install all those with

sudo apt install libsdl2-dev sudo apt install libsdl2-image-dev sudo apt install libsdl2-mixer-dev sudo apt install libsdl2-ttf-dev

Where do the headers get installed?

A quick search for sdl.h found it and associated header files in /usr/include/SDL2, a total of 76 header files though we’ll rarely use more than half a dozen.

Note. When I first wrote this, Ubuntu was on 18.04 LTS and clang was 6. When I updated this on 20.04 LTS, clang was version 10.0 and I got a weird include error.

The source code hasn’t changed but the VS Code JSON files have been altered slightly.

< "version": "2.0.0", "tasks": [ < "type": "shell", "label": "clang-10.0 build active file", "command": "/usr/bin/clang-10", "args": [ "-g", "$","$/hr_time.c", "-o", "$/demo", "-I/usr/include/SDL2", "-lSDL2" ], "options": < "cwd": "/usr/bin" >, "group": < "kind": "build", "isDefault": true >> ] >

The two changes between this and earlier versions are the command line to compile which is now /usr/bin/clang-10 though I think clang without the -10 will also do.

The second change luine is the addition of «-I/usr/include/SDL2», which tells the compiler the include path. Without it the compiler complains it can’t find the include file “begin_code.h” which is part of SDL2.

This is a listing of the demo program.

// sdldemo.c Author D. Bolton 7th March 2020 #include "hr_time.h" #include #include #define __timespec_defined 1 #define __timeval_defined 1 #define __itimerspec_defined 1 #include /* All SDL App's need this */ #include #include #define QUITKEY SDLK_ESCAPE #define WIDTH 1024 #define HEIGHT 768 SDL_Window* screen = NULL; SDL_Renderer *renderer; SDL_Event event; SDL_Rect source, destination, dst; int errorCount = 0; int keypressed; int rectCount = 0; stopWatch s; /* returns a number between 1 and max */ int Random(int max) < return (rand() % max) + 1; >void LogError(char * msg) < //FILE * err; //int error; //error = fopen_s(&err,"errorlog.txt", "a"); printf("%s\n", msg); //fclose(err); errorCount++; >/* Sets Window caption according to state - eg in debug mode or showing fps */ void SetCaption(char * msg) < SDL_SetWindowTitle(screen, msg); >/* Initialize all setup, set screen mode, load images etc */ void InitSetup() < srand((unsigned int)time(NULL)); SDL_Init(SDL_INIT_EVERYTHING); SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, SDL_WINDOW_SHOWN, &screen, &renderer); if (!screen) < LogError("InitSetup failed to create window"); >SetCaption("Example One"); > /* Cleans up after game over */ void FinishOff() < SDL_DestroyRenderer(renderer); SDL_DestroyWindow(screen); //Quit SDL SDL_Quit(); exit(0); >/* read a character */ char getaChar() < int result = -1; while (SDL_PollEvent(&event)) < if (event.type == SDL_KEYDOWN) < result = event.key.keysym.sym; break; >> return result; > void DrawRandomRectangle() < char buff[20]; SDL_Rect rect; SDL_SetRenderDrawColor(renderer, Random(256) - 1, Random(256) - 1, Random(256) - 1,255); rect.h = 200;// Random(100) + 20; rect.w = 200;// Random(100) + 20; rect.y = Random(HEIGHT - rect.h - 1); rect.x = Random(WIDTH - rect.w - 1); SDL_RenderFillRect(renderer,&rect); rectCount++; if (rectCount % 10000 == 0) < SDL_RenderPresent(renderer); stopTimer(&s); snprintf(buff,sizeof(buff),"%10.6f", diff(&s)); SetCaption(buff); startTimer(&s); >> /* main game loop. Handles demo mode, high score and game play */ void GameLoop() < int gameRunning = 1; startTimer(&s); while (gameRunning) < DrawRandomRectangle(); while (SDL_PollEvent(&event)) < switch (event.type) < case SDL_KEYDOWN: keypressed = event.key.keysym.sym; if (keypressed == QUITKEY) < gameRunning = 0; break; >break; case SDL_QUIT: /* if mouse click to close window */ < gameRunning = 0; break; >case SDL_KEYUP: < break; >> /* switch */ > /* while SDL_PollEvent */ > > int main(int argc,char * args[])

When you run this you’ll get a Window with random sized coloured rectangles like this and the time to draw 10,000 in the Window caption bar. Timing is done in the hr_time.h and .c files. These can be found in the file sdldemo.zip along with the VS code JSON files on GitHub.

Читайте также:  Динамический контроль целостности astra linux

Источник

How to install all SDL libraries in Ubuntu 14.04?

I don’t know whether SDL 1.2 is being installed on my system or not? Also, do I need to install sdl image, mixer, ttf etc etc? How to install them?

4 Answers 4

sudo apt-get install libsdl2-2.0 

and for installing everything necessary to build programs that use SDL:

sudo apt-get install libsdl2-dev 

How do I install dbg files? Mine can’t locate dbg packages. And when apt-cache search libsdl | grep dbg , nothing appears!

You can search for all available instances of libsld1.2 with something like the following:

andrew@corinth:~$ apt-cache search ^libsdl | grep 1.2 libsdl1.2-dbg - Simple DirectMedia Layer debug files libsdl1.2-dev - Simple DirectMedia Layer development files libsdl1.2debian - Simple DirectMedia Layer libsdl-image1.2-dbg - Image loading library for Simple DirectMedia Layer 1.2, debugging libsdl-mixer1.2-dbg - Mixer library for Simple DirectMedia Layer 1.2, debugging libsdl-net1.2-dbg - Network library for Simple DirectMedia Layer 1.2, debugging libsdl-gfx1.2-4 - drawing and graphical effects extension for SDL libsdl-gfx1.2-dev - development files for SDL_gfx libsdl-gfx1.2-doc - documentation files for SDL_gfx libsdl-image1.2 - Image loading library for Simple DirectMedia Layer 1.2, libraries libsdl-image1.2-dev - Image loading library for Simple DirectMedia Layer 1.2, development files libsdl-mixer1.2 - Mixer library for Simple DirectMedia Layer 1.2, libraries libsdl-mixer1.2-dev - Mixer library for Simple DirectMedia Layer 1.2, development files libsdl-net1.2 - Network library for Simple DirectMedia Layer 1.2, libraries libsdl-net1.2-dev - Network library for Simple DirectMedia Layer 1.2, development files libsdl-sound1.2 - Sound library for Simple DirectMedia Layer 1.2, libraries libsdl-sound1.2-dev - Sound library for Simple DirectMedia Layer 1.2, development files libsdl-ttf2.0-0 - TrueType Font library for Simple DirectMedia Layer 1.2, libraries libsdl-ttf2.0-dev - TrueType Font library for Simple DirectMedia Layer 1.2, development files andrew@corinth:~$ 

and then simply install all of the resulting files with something like the following:

sudo apt-get install libsdl1.2debian libsdl-gfx1.2-5 \ libsdl-gfx1.2-dev libsdl-gfx1.2-doc libsdl-image1.2 \ libsdl-image1.2-dbg libsdl-image1.2-dev libsdl-mixer1.2 \ libsdl-mixer1.2-dbg libsdl-mixer1.2-dev libsdl-net1.2 \ libsdl-net1.2-dbg libsdl-net1.2-dev libsdl-sound1.2 \ libsdl-sound1.2-dev libsdl-ttf2.0-0 libsdl-ttf2.0-dev 

You can easily leave some out such as the .dbg files which are simply debugging extras and you will not need the -dev files unless you are compiling. Some will also be dependencies of the others as well and will be automagically installed with installation of one of these files.

Читайте также:  All files read only linux

Источник

How to set up SDL2 on Linux

This article explains how to get started with SDL2 in Linux. For other SDL2 tutorials (including setting up on Windows), check out my SDL2 Tutorials at Programmer’s Ranch.

Using apt-get

If you’re on a Debian-based Linux distribution, you can use the APT package manager to easily install the SDL2 development libraries. From a terminal, install the libsdl2-dev package:

sudo apt-get install libsdl2-dev

Installing from source

If for whatever reason you can’t use a package manager, you’ll have to compile SDL2 from the source code. To do this, you first have to download SDL2:

sdl2linux-download

After extracting the archive to a folder, cd to that folder and run:

Testing it out

To verify that you can compile an SDL2 program, use the following code (it’s the same used in my “SDL2: Setting up SDL2 in Visual Studio 2010” article at Programmer’s Ranch):

#include int main(int argc, char ** argv)

You can use vi or your favourite editor to create the source code:

sdl2linux-minimal

To compile this (assuming it’s called sdl2minimal.c ), use the following command:

gcc sdl2minimal.c -lSDL2 -lSDL2main -o sdl2minimal

We need to link in the SDL2 libraries, which is why we add the -lSDL2 -lSDL2main . Be aware that those start with a lowercase L, not a 1. The program should compile. It won’t show you anything if you run it, but now you know that you’re all set up to write SDL2 programs on Linux.

sdl2linux-run

7 thoughts on “How to set up SDL2 on Linux”

I found this very helpful, thanks for the post. I thought setting up libsdl would be much more difficult but I got through this with no problems in under 7 minutes.

Hi, I just wanted to say thank you!
It worked like a charm and now I’m off to the races with some SDL2 action 😀 Best wishes from Germany

Thanks for the straight-forward, concise instructions on how to install SDL2 on Linux. I initially tried using the “apt” method, but it failed on my particular Linux distro, so it was great that you also included the “make” method as well.

This isn’t a Linux tutorial, it’s an Ubuntu tutorial and should be labelled as such. Really tiresome to see rank amateurs poisoning open source like this.

What’s tiresome is ungrateful and arrogant people being rude without knowing what they’re talking about. There is no mention of Ubuntu in the article. The apt-get instructions work on any Debian-based distribution (the screenshots are from Linux Mint), and installing from source works on any distribution. Who’s the amateur now? A public apology would be in order.

Источник

Оцените статью
Adblock
detector