7

Update: issue is solved. The library was Made for Armv7a CPUs but it was "soft float" Not "hard float". It seems like my machine is HF and Not SF compatible

My program depends on an externally build .so library called libMyLib.so. When I compile the program like this:

$ g++ -std=c++11 main.cpp -o run -pthread

it reports that there are a lot of undefined references, obviously because i didn't include libMyLib.so when compiling. So the compiler knows what he needs to compile the program. When i compile the program like this:

$ g++ -std=c++11 main.cpp -o run -pthread -lMyLib

it doesn't report any errors and creates the file "run". Notice that libMyLib.so is already in /usr/local/lib and it looks like it is linked when compiling since the references are defined now and the "run" file is created. But as i run the file, this happens:

$ ./run
    ./run: error while loading shared libraries: libMyLib.so: cannot open shared object file: No such file or directory

I've checked with ldd and it shows me this:

$ ldd run
    ...
    libMyLib.so => not found
    ...

So ldd doesn't find the library on execution, but it finds it while compiling. I'm quite new to Linux and linking libraries so i don't know what to do.

Also, running ldd on the .so file returns this:

$ ldd /usr/local/lib/libMyLib.so
    not a dynamic executable

I've already checked an this message may occur when running a .so file on the wrong platform. But i've checked, the library is compiled for arm (I'm running on a raspberry pi -> arm):

$  objdump -f /usr/local/lib/libMyLib.so | grep ^architecture
    architecture: arm, flags 0x00000150:

I also update the linker:

$ sudo ldconfig -v
...
/usr/local/lib:
    libwiringPi.so -> libwiringPi.so.2.44
    libwiringPiDev.so -> libwiringPiDev.so.2.44
    libMyLib.so -> libMyLib.so.1
...

I really have no clue why this might still happen. Can anyone help me?

2 Answers 2

8

/usr/local/lib is one of the directories that the linker searches by default for libraries specified with the -l option, so your linkage succeeds.

At runtime however, the program loader by default searches for the linked libraries in:-

  • /lib, /usr/lib and among the libraries whose names and locations have been cached in the ldconfig cache, /etc/ld.so.cache.
  • The directories listed in the value of the environment variable LD_LIBRARY_PATH, in the current shell.

The ldconfig cache is only updated when ldconfig is run. See man ldconfig.

The loader fails to find libMyLib.so at runtime because you have not run ldconfig since you placed that library in /usr/local/lib and neither have you correctly added /usr/local/lib to the LD_LIBRARY_PATH in the same shell in which you try to run the program.

It is inconvenient and otherwise undesirable to require a special setting of LD_LIBRARY_PATH to enable a program to run.

To enable the loader to find your library, run ldconfig as root. This will succeed provided that /usr/local/lib is listed in /etc/ld.so.conf, or in one of the files included by /etc/ld.so.conf. If it's not, then you can explicitly cache the shared libraries from /usr/local/lib by running ldconfig /usr/local/lib, as root.

Sign up to request clarification or add additional context in comments.

1 Comment

This solved my problem, but how do I configure cmake to install my software in /lib rather than /usr/local/lib. Giving root as prefix seems absurd. I don't want unecessary folders in my root folder.
5

First check LD_LIBRARY_PATH variable is having the path to your library directory

$ echo $LD_LIBRARY_PATH

If not there then update the library path.

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/library

Use strace to debug.

strace -f ./run

13 Comments

LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH ./run try this
thanks for your support, but this doesn't do the trick either. Could this happen due to missing dependencies in the .so library?
2 options you can try . 1 strace command and 2. objdump -f /usr/local/lib/libMyLib.so | grep NEEDED and check the dependance library if exist.
Already checked out option 2. Option 1 shows an output like this: open("/usr/lib/tls/v7l/neon/vfp/libMyLib.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)\n stat64("/usr/lib/tls/v7l/neon/vfp", 0x7eb967b8) = -1 ENOENT (No such file or directory) ..... It does this with several path combinations and checks several paths like /usr/lib, usr/local/lib, ... I'm checking the result on google right now
Two questions open("/path/to/library/libMyLib.so") does this come up in the strace and the permission of the libMyLib.so is same as other libraries.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.