Search code examples
c++linuxgccsdl-2sdl-ttf

Linux binary cannot find local shared library with -rpath set to $ORIGIN


Over the last few months, I have been making a game with C++ and SDL2 (a graphics library). The game is finished now, and I want to release it. The Windows build seems to work fine, but I am having issues with building it for Linux. Since SDL2 (and SDL2_ttf, which is responsible for text rendering) are not standard libraries, I ship the .so files with the binary and dynamically link to them, but when I run the application on another Linux system (without SD2_ttf installed), the program does not launch. When I run the "readelf -d" command on the binary, it shows me that the runpath is indeed $ORIGIN, but the "ldd" command tells me that the libraries were not found. I have been searching for the solution for several days now, and I cannot understand what I'm doing wrong. I wrote the program in vscode on Linux Mint, if that somehow makes a difference.

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-no-pie",
                "-static-libstdc++",
                "-static-libgcc",
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-Wl,-R,'$ORIGIN'",
                "-lSDL2main",
                "-lSDL2",
                "-lSDL2_ttf"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

First few lines of ldd output on fresh Linux Mint install (running in virtual machine). SDL2 seems to be included in Linux Mint by default, but SDL2_ttf is not, and I would rather have these libraries loaded from the app folder:

linux-vdso.so.1 (0x00007ffc82c52000)
libSDL2-2.0.so.0 => /lib/x86_64-linux-gnu/libSDL2-2.0.so.0 (0x00007f696c4ff000)
libSDL2_ttf-2.0.so.0 => not found

readelf -d output:


Dynamic section at offset 0x11ed98 contains 28 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libSDL2-2.0.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libSDL2_ttf-2.0.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN]
 0x000000000000000c (INIT)               0x404000
 0x000000000000000d (FINI)               0x4d96c8
 0x0000000000000019 (INIT_ARRAY)         0x518890
 0x000000000000001b (INIT_ARRAYSZ)       72 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x5188d8
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x4003e8
 0x0000000000000005 (STRTAB)             0x4014d8
 0x0000000000000006 (SYMTAB)             0x400410
 0x000000000000000a (STRSZ)              2182 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x520000
 0x0000000000000002 (PLTRELSZ)           4080 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x402040
 0x0000000000000007 (RELA)               0x401f98
 0x0000000000000008 (RELASZ)             168 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x401ec8
 0x000000006fffffff (VERNEEDNUM)         2
 0x000000006ffffff0 (VERSYM)             0x401d5e
 0x0000000000000000 (NULL)               0x0


Game folder contents: Game folder


Solution

  • As amonakov pointed out, the names of my .so files contained .18.0 at the end, because of which the linker could not find it. If anybody has the same issue, check that.