#undefined reference to `SDL_Init` when linking to SDL2 with pkg-config --libs

1 messages · Page 1 of 1 (latest)

latent pecan
#

Hi all!
I am doing this from the UCRT64 environment. To demonstrate my problem, I'm building a source file (example.c) with the following content:

#include <SDL.h>
int main(int argc, char **argv) {
        SDL_Init(SDL_INIT_VIDEO);
        SDL_Quit();
        return 0;
}

Using these commands:

x86_64-w64-mingw32-gcc $(pkg-config --cflags SDL2) -c example.c -o example.o
x86_64-w64-mingw32-gcc $(pkg-config --libs SDL2) example.o -o example

For reference, they expand as follows:

x86_64-w64-mingw32-gcc -Dmain=SDL_main -IC:/msys64/ucrt64/include/SDL2 -c example.c -o example.o
x86_64-w64-mingw32-gcc -lmingw32 -mwindows -lSDL2main -lSDL2 example.o -o example

Compiling works, but when I get to linking I get the following list of errors:

C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example.o:example.c:(.text+0x15): undefined reference to `SDL_Init'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example.o:example.c:(.text+0x1a): undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status

I believe I have the correct libraries and compiler installed:

  • mingw-w64-ucrt-x86_64-gcc 12.2.0-4
  • mingw-w64-ucrt-x86_64-gcc-libs 12.2.0-4
  • mingw-w64-ucrt-x86_64-SDL2 2.24.1-1

Does anyone have any ideas of what I might be doing wrong? Probably something simple...😂

lapis sedge
#

Try this x86_64-w64-mingw32-gcc example.o $(pkg-config --libs SDL2) -o example