user34537
user34537

Reputation:

Can't load dll in linux gcc

I'm trying to compile my code with a DLL i made and I get the error below when i write ./prog

./prog: error while loading shared libraries: libctest.so.1: cannot open shared object file: No such file or directory

I followed the tutorial here and my mono app has no problem loading the dll and calling the functions. The key parts of the tutorial were

gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so

My C# code does

[DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
public static extern void test();

I built with

gcc -Wall -L/opt/lib main.c -lctest -o prog

This is the first thing i changed. prog.c to main.c. From there I simply run with ./prog to get the error at the top of this question. I do this as root so there shouldn't be permission issues. I also chmod 755 the so's. What am I doing wrong? This is opensuse. I had to create the /opt/lib so I am thinking this path isn't registered where it should be

Upvotes: 0

Views: 905

Answers (1)

Austin Phillips
Austin Phillips

Reputation: 15776

The dynamic linker ld.so will not look in /opt/lib by default when attempting to resolve library dependencies. You have to tell the linker about the non-standard library directories or add the /opt/lib path to your prog binary.

eg

LD_LIBRARY_PATH=/opt/lib ./prog

or, link prog with the -rpath linker option. This provides an additional path for the linker to search when resolving locations of shared objects.

gcc -Wall -L/opt/lib -Wl,-rpath,/opt/lib main.c -lctest -o prog

You can use the ldd command on a binary to show the locations of shared libraries.

ldd prog

This will currently show that libctest.so cannot be found. With an additional search path, the following command should show that all libraries are resolved:

LD_LIBRARY_PATH=/opt/lib ldd prog

Upvotes: 3

Related Questions