Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c++ - Libraries in /usr/local/lib not found

I am building an application using a framework called ohNet. After building the framework, there is the possibility to install the framework via make install. By default the libraries are installed inside the /usr/local/[lib|include] folders. ok.

I am using eclipse for development. In order to use this libraries I have to set the include path to the library (in this case usr/local/include/ohNet), set the Linker search path (-L)(/usr/local/lib/ohNet) and specific libraries (-l) (in this case i choose a library called libohNet.so which is in this folder. When I build the project in eclipse it works fine, however if i try to run the programm i am faced with the following message:

error while loading shared libraries: libohNet.so: cannot open shared object file: No such file or directory

I've double checked this, and the file libohNet.so is in this directory! What's the reason that this file cannot be found?

I searched on google and found some posts, saying that it is problematic that libraries are getting installed into /usr/local/lib instead of /usr/lib see here ... Do I have to configure some additional settings in eclipse to make ld recognize libraries in this path? What's the solution for this?

regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a runtime error, not a build error. Setting the -L flag does nothing for the runtime linker. What you need to do is to tell the runtime loader to also look in /usr/local/lib for libraries. You can do that in two ways. The first is to add the path to the LD_LIBRARY_PATH environment variable:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"

The second is to update the configuration file of the runtime linker. This can happen either in the /etc/ld.so.conf file, by putting the line:

/usr/local/lib

somewhere in that file, or by creating a new *.conf file in the /etc/ld.so.conf.d/ directory that contains the new path. For example:

/etc/ld.so.conf.d/99local.conf

with just:

/usr/local/lib

in it. This is the recommended way of doing this, as it allows you to keep your custom library paths separate from paths set by the system. (The "99" prefix is there to make sure the file is loaded last compared to other files there, so that it won't preempt system paths that could contain the same libraries.)

After you modify/create the file in /etc, you need to run:

ldconfig

as root for the change to take effect. (This command updates the /etc/ld.so.cache file, which is the actual file used by the runtime linker.)

There's also another way for a binary to find needed libraries at runtime. You can actually hard-code library paths into the executable itself. This is accomplished by setting a so called "rpath". This is a linker option and must be passed from gcc (or g++) to the linker, so the -Wl option has to be used. The linker option is -rpath=PATH. So you would need to add this to your link flags:

-Wl,-rpath=/usr/local/lib

I don't recommend this for your case though. An rpath is useful when you're shipping libraries together with your executable (maybe with an installer), and a relative rpath (using the rpath $ORIGIN feature) or absolute one (for when you install in /opt, for example) is then used to find those bundled libs at runtime.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...