<p>The problem is definitely a lack of linker flags.</p>
<p>It can see that you want to include &lt;SDL/SDL2.h&gt;, which is a sort of promise that said functions contained in that header have been built and will be available; but when your linker looks to find the actual machine code, it needs a little more than the header. It needs the <em>library</em> itself.</p>
<p>For SDL2, you would usually use something like:</p>
<pre><code>gcc -g main.c -o ../ludo -lSDL2
</code></pre>
<p>On Linux, this is more-or-less the same command, but for complete definitions it also instructs the compiler to look inside <code>/usr/lib/x86_64-linux-gnu/libSDL2.a</code> (or wherever it is on your system). Note that the <code>lib</code> prefix on the file name is assumed, and the extension is irrelevant.</p>
<p>Do that, and your program should compile. Note that some SDL extensions, like Image and Mixer, require their own libraries. An SDL-specific shortcut to ensure that you've got everything is to use the handy <code>pkg-config</code>, like so:</p>
<pre><code>gcc -g main.c -o ../ludo $(pkg-config --cflags --libs sdl2)
</code></pre>
<p>Which will be interpreted by the terminal as</p>
<pre><code>gcc -g main.c -o ../ludo -D_REENTRANT -I/usr/include/SDL2 -lSDL2
</code></pre>
<p>This is less generic however, a bit more paranoid, and a bit of a cheat; but perhaps it will help you.</p>
<p>The thing to remember with C is that a function needs to be <em>declared</em> to be used in code, and that's compiler-end; but to be built into an actual stand-alone program it also has to be <em>defined</em>, which is where the post-compiler linker comes in.</p>

CC BY-SA 4.0
2020-09-19T12:37:45.167Z
2020-09-19T12:37:45.167Z
Michael Macha
<p>I'm trying to learn computer graphics, but when I tried to get this very simple windower running with c</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#include &lt;SDL2/SDL.h&gt;

static const int width = 320;
static const int height = 180;

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}
</code></pre>

<p>I try to compile and...</p>

<pre><code>vke@komputilo:~/dev/MiaLudo/source$ gcc -g main.c -o ../ludo
/tmp/ccfVU5ua.o: In function `main':
/home/vke/dev/MiaLudo/source/main.c:11: undefined reference to `SDL_Init'
/home/vke/dev/MiaLudo/source/main.c:13: undefined reference to `SDL_CreateWindow'
/home/vke/dev/MiaLudo/source/main.c:15: undefined reference to `SDL_DestroyWindow'
/home/vke/dev/MiaLudo/source/main.c:16: undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status
</code></pre>

<p>Hoping someone can help me out, I'm running Trisquel Linux if that's important</p>

CC BY-SA 4.0
2019-06-18T08:09:04.047Z
Being pedantic, the compilation looks fine, but you are getting a linker error.  Presumably you'll need a "-l" (lower case L) linker option on the gcc line that points to the library that contains the SDL_xxxx functions. 
This, https://stackoverflow.com/a/17886993/626644 , suggests you need -lSDL  (noting again the first char is a lowercase L)
Simon F
CC BY-SA 4.0
2019-06-18T08:21:44.887Z
You might need to add a search path as well. (FWIW I've not used this library)
Simon F
<opengl>
sdl2: Compiler throws "Undefined Reference"
12Bits
# sdl2: Compiler throws "Undefined Reference"

(Asked by: username_0 on 2019-06-17)

I'm trying to learn computer graphics, but when I tried to get this very simple windower running with c

```
#include <stdio.h>
#include <stdlib.h>

#include <SDL2/SDL.h>

static const int width = 320;
static const int height = 180;

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

```

I try to compile and...

```
vke@komputilo:~/dev/MiaLudo/source$ gcc -g main.c -o ../ludo
/tmp/ccfVU5ua.o: In function `main':
/home/vke/dev/MiaLudo/source/main.c:11: undefined reference to `SDL_Init'
/home/vke/dev/MiaLudo/source/main.c:13: undefined reference to `SDL_CreateWindow'
/home/vke/dev/MiaLudo/source/main.c:15: undefined reference to `SDL_DestroyWindow'
/home/vke/dev/MiaLudo/source/main.c:16: undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

```

Hoping someone can help me out, I'm running Trisquel Linux if that's important

**Tags:** opengl

> **Comments:**
> **username_1**: Being pedantic, the compilation looks fine, but you are getting a linker error.  Presumably you'll need a "-l" (lower case L) linker option on the gcc line that points to the library that contains the SDL_xxxx functions.  This, https://stackoverflow.com/a/17886993/626644 , suggests you need -lSDL  (noting again the first char is a lowercase L)  
> **username_1**: You might need to add a search path as well. (FWIW I've not used this library)  

---

## 1 answers

### Answer by username_2 on 2020-09-19. Score: 1

The problem is definitely a lack of linker flags.

It can see that you want to include <SDL/SDL2.h>, which is a sort of promise that said functions contained in that header have been built and will be available; but when your linker looks to find the actual machine code, it needs a little more than the header. It needs the *library* itself.

For SDL2, you would usually use something like:

```
gcc -g main.c -o ../ludo -lSDL2

```

On Linux, this is more-or-less the same command, but for complete definitions it also instructs the compiler to look inside `/usr/lib/x86_64-linux-gnu/libSDL2.a` (or wherever it is on your system). Note that the `lib` prefix on the file name is assumed, and the extension is irrelevant.

Do that, and your program should compile. Note that some SDL extensions, like Image and Mixer, require their own libraries. An SDL-specific shortcut to ensure that you've got everything is to use the handy `pkg-config`, like so:

```
gcc -g main.c -o ../ludo $(pkg-config --cflags --libs sdl2)

```

Which will be interpreted by the terminal as

```
gcc -g main.c -o ../ludo -D_REENTRANT -I/usr/include/SDL2 -lSDL2

```

This is less generic however, a bit more paranoid, and a bit of a cheat; but perhaps it will help you.

The thing to remember with C is that a function needs to be *declared* to be used in code, and that's compiler-end; but to be built into an actual stand-alone program it also has to be *defined*, which is where the post-compiler linker comes in.


---

