gcc – Can’t link SDL2 library on Raspberry Pi OS (Debian)

I am attempting to compile a trivial SDL2 app on Raspberry Pi OS, which is based on Debian.

I installed SDL2 via sudo apt-get install libsdl2-dev and now have SDL.h in /usr/include/SDL and libSDL2.a in /usr/lib/aarch64-linux-gnu.

I have included -lSDL2, which I confirmed with pkg-config --libs sdl2, but the linker still reports undefined references for things like SDL_Init.

Full source:

#include 
#include 

int main() {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "failed to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Quit();
    return 0;
}

Build commands:

mkdir obj
gcc -std=c99 -O2 -Wall -Wextra -c -I/usr/include/SDL2 test.c -o obj/test.o
gcc -Wall -Wextra -L/usr/lib/aarch64-linux-gnu -lSDL2 obj/test.o -o test
/usr/bin/ld: obj/test.o: in function `main':
test.c:(.text.startup+0xc): undefined reference to `SDL_Init'
/usr/bin/ld: test.c:(.text.startup+0x14): undefined reference to `SDL_Quit'
/usr/bin/ld: test.c:(.text.startup+0x34): undefined reference to `SDL_GetError'

Compiler:

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/aarch64-linux-gnu/12/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 12.2.0-14' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=aarch64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --enable-fix-cortex-a53-843419 --disable-werror --enable-checking=release --build=aarch64-linux-gnu --host=aarch64-linux-gnu --target=aarch64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd

Any ideas? Thanks in advance!

Read more here: Source link