Skip to content
Advertisement

Why won’t my compiled c++ binary execute?

So I have just compiled my code and when I try to execute the binary I am getting

-bash: ./a.out: No such file or directory

When I do ldd a.out I see everything is there that I need

ldd a.out
   linux-vdso.so.1 (0x00007ffd337fb000)
   libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f1200930000)
   librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f1200728000)
   libncurses.so.5 => /lib/x86_64-linux-gnu/libncurses.so.5 (0x00007f1200505000)
   libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f12002db000)
   libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f12000d7000)
   libcap.so.2 => /lib/x86_64-linux-gnu/libcap.so.2 (0x00007f11ffed1000)
   libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007f11ffcb7000)
   libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f11ff919000)
   libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f11ff6fa000)
   libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f11ff309000)
   /usr/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f1200b4d000)

Also doing an ls -la shows the file as such

lrwxrwxrwx 1 user user    33 Jul 18 21:26 a.out

Does anyone have any reason why this isn’t working?

I am using Ubuntu 18.04.2 LTS.

Advertisement

Answer

When I do ldd a.out I see everything is there that I need

No, there isn’t.

ldd has been changed to run a known ELF interpreter / dynamic linker instead of the one from the binary’s header. You can check that by yourself — ldd is a shell script on your system.

ldd a.out
...
/usr/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2

And here it’s the proof. Do you have a /usr/lib/ld-linux-x86-64.so.2 on your system?

A simpler testcase:

$ echo 'int main(){}' | cc -xc -
$ ./a.out
$ perl -pe 's/ld-linux/ld-LOOOL/' -i ./a.out
$ ./a.out
bash: ./a.out: No such file or directory
$ ldd ./a.out
        linux-vdso.so.1 (0x00007ffd707e9000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe936d52000)
        /lib64/ld-LOOOL-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007fe9372f3000)

As a quick-fix, you can pass the path to an existing interpreter when compiling your program

cc -Wl,-dynamic-linker,/lib64/ld-linux-x86-64.so.2 ...
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement