1

How can I compile a sdl project using gcc in the linux command line without using Cmake?

EDIT;

gcc SDLGAME.c pkg-config --cflags --libs sdl2

but i get error.

gcc: error: Pkg-config: No such file or directory
gcc: error: sdl2: No such file or directory
gcc: error: unrecognized command line option ‘--cflags’
gcc: error: unrecognized command line option ‘--libs’; did you mean ‘--libs=’?

@HolyBlackCat

source code --->>


#include <stdio.h>
#include <SDL2/SDL.h>

int main(int argc,char *argv[])
{

    if (SDL_Init(SDL_INIT_VIDEO) !=0)
    {
        printf("error SDL");
        return 0;
    }   
    SDL_Window* win=SDL_CreateWindow("Game",
                    SDL_WINDOWPOS_CENTERED,
                    SDL_WINDOWPOS_CENTERED,
                    500,500,0);
    return 0;
}

I get this error--->>

error: XDG_RUNTIME_DIR not set in the environment.
error SDL
gecek sila
  • 21
  • 3
  • 1
    Does this answer your question? [How to compile an example SDL program written in C?](https://stackoverflow.com/questions/19366064/how-to-compile-an-example-sdl-program-written-in-c) – Paul Hankin Apr 23 '21 at 16:16
  • Or: https://stackoverflow.com/questions/64396979/ – genpfault Apr 23 '21 at 16:21
  • I couldn't do it – gecek sila Apr 23 '21 at 16:47
  • "couldn't do it" is not a problem description. – HolyBlackCat Apr 23 '21 at 16:47
  • Try running `pkg-config --cflags --libs sdl2`, it will tell you the flags. Then add those flags at the end of your usual `gcc ...` command. – HolyBlackCat Apr 23 '21 at 16:49
  • I tried but got an error @HolyBlackCat – gecek sila Apr 23 '21 at 17:00
  • No, your version of ubuntu should be fine. Leave it as is. `XDG_RUNTIME_DIR` is defined on _both_ my systems as `/run/user/1000` (1000 is my user id). I did: `export XDG_RUNTIME_DIR=/tmp/dir ; mkdir -p /tmp/dir` and it also ran okay. But, I did `unset XDG_RUNTIME_DIR` and it _still_ ran okay. I may have built and installed `SDL2` from _source_ on ubuntu because [IIRC] I had some issue about a year ago. – Craig Estey Apr 23 '21 at 17:39
  • Try my first hack: `export XDG_RUNTIME_DIR=/tmp/dir ; mkdir -p /tmp/dir`. But, `SDL2` _should_ run without it. Okay, I just double checked. On my fedora system, SDL2 is installed from the binary package. But ... On my ubuntu system, I _did_ download the _source_ package and rebuild and install from that. (On ubuntu, it was reinstalled in `/usr/local`) – Craig Estey Apr 23 '21 at 17:45
  • See: https://askubuntu.com/questions/28372/how-do-i-get-and-modify-the-source-code-of-packages-installed-through-apt-get for info on download/build of source packages – Craig Estey Apr 23 '21 at 17:48
  • I did what it said, but this time I get the error "error SDL" in the if condition. @CraigEstey – gecek sila Apr 23 '21 at 18:15
  • 1
    On Linux with the `pkg-config` tool and package, compile your `SDLGAME.c` file with `gcc -Wall -Wextra -g SDLGAME.c $(pkg-config --cflags --libs sdl2) -o sdlgame-binary` then use the GDB debugger – Basile Starynkevitch Apr 23 '21 at 20:02

1 Answers1

1

Caveat: This isn't a total solution but some suggestions and is prefaced by top comment's and comments under OP's [now deleted] answer.


To review ...

After fixing the original issue by use of:

gcc -o SDLGAME SDLGAME.c `pkg-config --cflags --libs sdl2`

OP running the program produces:

error: XDG_RUNTIME_DIR not set in the environment.

This may be a general issue about the ubuntu install itself. Some resources for that: https://askubuntu.com/questions/872792/what-is-xdg-runtime-dir and https://askubuntu.com/questions/456689/error-xdg-runtime-dir-not-set-in-the-environment-when-attempting-to-run-naut

A workaround may be:

export XDG_RUNTIME_DIR=/tmp/dir
mkdir -p /tmp/dir

But, I ran the program successfully on my home system, running fedora 29 and my ubuntu system running 18.04.5

On my systems, XDG_RUNTIME_DIR was set to /run/user/1000. However, with/without the workaround and even doing unset XDG_RUNTIME_DIR worked on my systems.

However: On my ubuntu system, I had removed the standard libsdl2 package and rebuilt and installed it from the source package a year ago due to some issues I had.

So, if the workaround doesn't work, I recommend libsdl2 rebuild/reinstall from source.

Even if the standard package is working, when debugging your app, it can be helpful to be able to consult the libsdl2 source.

Note that one change I made to your app was to add a sleep(3) at the bottom so you can see the window come up.


Here is the method I used to build/install from source:

It's probably necessary to uninstall/remove the binary libsdl2 package. So, you'll have to do (e.g.)

sudo apt-get remove libsdl2 libsdl2-dev

Or, whatever the binary package is called [I forget]. But, those also came from: apt-cache search libsdl2

So, once that's cleaned out, what I did was:

  1. Create a directory (e.g.): $HOME/aptsrc
  2. cd $HOME/aptsrc
  3. Download the source package [without sudo]: apt-get source libsdl2
  4. This extracts several files (e.g. *.tar.gz, *.tar.xz, *.dsc and a directory. On my system, it was: libsdl2-2.0.8+dfsg1, but for you it may be different. Do (e.g.): DIR=$HOME/aptsrc/libsdl2-2.0.8+dfsg1
  5. cd $DIR
  6. Configure with: $DIR/configure
  7. Run cmake: cmake $DIR
  8. Run make with: make
  9. Install with: sudo make install

Note that this comes from an internal script I created. Even after the cd $DIR, I think it's necessary to use full path on the commands [where indicated].

Now, the library should be installed under /usr/local. The output of pkg-config --cflags --libs sdl2 should reflect this:

-D_REENTRANT -I/usr/local/include/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2

The original output of this command would have looked like:

-I/usr/include/SDL2 -D_REENTRANT -lSDL2

This is for the standard install from the binary package, so if you still have that, the binary package may still be installed.

Otherwise, you should now be able to rebuild your app using the original gcc command. Now, it should be attached to the source built version of the library. You can confirm this with: ldd ./SDLGAME but just running it might be easier.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48