C++ 找不到 /usr/local/lib 中的库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17889799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:31:00  来源:igfitidea点击:

Libraries in /usr/local/lib not found

c++eclipsebuildlinkerlibraries

提问by Moonlit

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.

我正在使用名为ohNet的框架构建应用程序。构建框架后,可以通过make install. 默认情况下,库安装在/usr/local/[lib|include]文件夹内。好的。

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.sowhich isin 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:

我正在使用eclipse进行开发。为了使用这个库,我必须设置库的包含路径(在这种情况下usr/local/include/ohNet),设置链接器搜索路径(-L)(/usr/local/lib/ohNet)和特定库(-l)(在这种情况下,我选择一个名为libohNet.sowhich此文件夹中。当我在Eclipse中构建它工作正常的项目,但如果我尝试运行我面对以下消息PROGRAMM:

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.sois in this directory! What's the reason that this file cannot be found?

我已经仔细检查过这个,文件libohNet.so在这个目录中!找不到这个文件的原因是什么?

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

我在谷歌上搜索并找到了一些帖子,说安装库/usr/local/lib而不是在/usr/lib这里看到是有问题的......我是否必须配置一些额外的设置eclipse才能ld在此路径中识别库?对此有什么解决方案?

regards

问候

回答by Nikos C.

This is a runtime error, not a build error. Setting the -Lflag 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_PATHenvironment variable:

这是运行时错误,而不是构建错误。设置-L标志对运行时链接器没有任何作用。您需要做的是告诉运行时加载程序也在 /usr/local/lib 中查找库。您可以通过两种方式做到这一点。首先是添加LD_LIBRARY_PATH环境变量的路径:

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:

二是更新运行时链接器的配置文件。这可能发生在 /etc/ld.so.conf 文件中,通过放置以下行:

/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/ 目录中创建一个新的 *.conf 文件。例如:

/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.)

在里面。这是推荐的方法,因为它允许您将自定义库路径与系统设置的路径分开。(“99”前缀是为了确保该文件与那里的其他文件相比是最后加载的,这样它就不会抢占可能包含相同库的系统路径。)

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

在 /etc 中修改/创建文件后,您需要运行:

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.)

以 root 身份使更改生效。(此命令更新 /etc/ld.so.cache 文件,这是运行时链接程序使用的实际文件。)

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 -Wloption has to be used. The linker option is -rpath=PATH. So you would need to add this to your link flags:

二进制文件还有另一种在运行时查找所需库的方法。您实际上可以将库路径硬编码到可执行文件本身中。这是通过设置所谓的“rpath”来实现的。这是一个链接器选项,必须从 gcc(或 g++)传递给链接器,因此-Wl必须使用该选项。链接器选项是-rpath=PATH. 因此,您需要将其添加到链接标志中:

-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 $ORIGINfeature) or absolute one (for when you install in /opt, for example) is then used to find those bundled libs at runtime.

不过,我不建议在您的情况下使用此方法。当您将库与可执行文件(可能带有安装程序)一起发送时,rpath 很有用,然后使用相对 rpath(使用该rpath $ORIGIN功能)或绝对路径(例如,当您安装在 /opt 中时)用于查找那些在运行时捆绑的库。