user3520478
user3520478

Reputation: 33

Error loading shared library in C

I'm trying to use the command:

gcc -I${HOME}/usr/include -L${HOME}/usr/lib -lsodium test.c

but when I try and run a.out it gives the error:

./a.out: error while loading shared libraries: libsodium.so.4: cannot open shared object file: No such file or directory

but libsodium.so.4 is definitely in the ${HOME}/usr/lib directory. What's going on? test.c is just

#include <stdio.h>
#include "sodium.h"
int main(int argc, char** argv)
{ return (0); }

Upvotes: 2

Views: 3462

Answers (3)

tkocmathla
tkocmathla

Reputation: 901

${HOME}/usr/lib is not in your runtime library path.

You can bake the path into your executable using the gcc option -Wl,-rpath,${HOME}/usr/lib or set the environment variable LD_LIBRARY_PATH=${HOME}/usr/lib before executing the program.

ldd a.out will tell you if libsodium can be found in your runtime library path, and if so, the location of the library.

Upvotes: 3

nephtes
nephtes

Reputation: 1319

You need to tell the runtime linker where to find the .so. Typically this is done with the LD_LIBRARY_PATH environment variable, so you would invoke a.out like so (assuming you're using a bash-like shell):

LD_LIBRARY_PATH=${HOME}/usr/lib ./a.out

Upvotes: 2

Lee Duhem
Lee Duhem

Reputation: 15121

Please

export LD_LIBRARY_PATH=${HOME}/usr/lib

first and try again.

That export ... will tell loader (ld-linux.so) also search ${HOME}/usr/lib for shared libraries.

Upvotes: 1

Related Questions