問題描述
我正在使用名為 ohNet 的框架構(gòu)建應(yīng)用程序.構(gòu)建框架后,可以通過 make install
安裝框架.默認(rèn)情況下,這些庫安裝在 /usr/local/[lib|include]
文件夾中.好的.
我正在使用 Eclipse 進(jìn)行開發(fā).為了使用這個(gè)庫,我必須設(shè)置庫的包含路徑(在本例中為 usr/local/include/ohNet
),設(shè)置鏈接器搜索路徑 (-L)(/usr/local/lib/ohNet
) 和特定的庫 (-l).當(dāng)我在 Eclipse 中構(gòu)建項(xiàng)目時(shí),它工作正常,但是如果我嘗試運(yùn)行該程序,我將面臨以下消息:
加載共享庫時(shí)出錯(cuò):libohNet.so:無法打開共享對象文件:沒有這樣的文件或目錄
我已經(jīng)仔細(xì)檢查過,文件 libohNet.so
就在這個(gè)目錄中!找不到這個(gè)文件是什么原因?
我在google上搜索,發(fā)現(xiàn)一些帖子,說庫安裝到/usr/local/lib
而不是/usr/lib
是有問題的看這里 ...我是否必須在 eclipse
中配置一些其他設(shè)置以使 ld
識別此路徑中的庫?有什么辦法解決這個(gè)問題?
問候
這是運(yùn)行時(shí)錯(cuò)誤,不是構(gòu)建錯(cuò)誤.設(shè)置 -L
標(biāo)志對運(yùn)行時(shí)鏈接器沒有任何作用.您需要做的是告訴運(yùn)行時(shí)加載程序也在/usr/local/lib 中查找?guī)?你可以通過兩種方式做到這一點(diǎn).首先是添加LD_LIBRARY_PATH
環(huán)境變量的路徑:
二是更新運(yùn)行時(shí)鏈接器的配置文件.這可以在/etc/ld.so.conf 文件中發(fā)生,方法是:
<上一頁>/usr/local/lib在該文件的某個(gè)位置,或者通過在包含新路徑的/etc/ld.so.conf.d/目錄中創(chuàng)建一個(gè)新的 *.conf 文件.例如:
<上一頁>/etc/ld.so.conf.d/99local.conf只需:
<上一頁>/usr/local/lib在里面.這是這樣做的推薦方式,因?yàn)樗试S您將自定義庫路徑與系統(tǒng)設(shè)置的路徑分開.(99"前綴是為了確保文件與那里的其他文件相比最后加載,這樣它就不會搶占可能包含相同庫的系統(tǒng)路徑.)
在/etc中修改/創(chuàng)建文件后,需要運(yùn)行:
<上一頁>配置文件以 root 身份使更改生效.(此命令更新/etc/ld.so.cache 文件,這是運(yùn)行時(shí)鏈接器使用的實(shí)際文件.)
二進(jìn)制文件還有另一種方法可以在運(yùn)行時(shí)找到所需的庫.您實(shí)際上可以將庫路徑硬編碼到可執(zhí)行文件本身中.這是通過設(shè)置所謂的rpath"來實(shí)現(xiàn)的.這是一個(gè)鏈接器選項(xiàng),必須從 gcc(或 g++)傳遞到鏈接器,因此必須使用 -Wl
選項(xiàng).鏈接器選項(xiàng)是 -rpath=PATH
.因此,您需要將其添加到您的鏈接標(biāo)志中:
不過,我不建議您這樣做.當(dāng)您將庫與可執(zhí)行文件(可能帶有安裝程序)以及相對 rpath(使用 rpath $ORIGIN
功能)或絕對路徑(當(dāng)您安裝在/opt,例如)然后用于在運(yùn)行時(shí)查找那些捆綁的庫.
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
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.
這篇關(guān)于未找到/usr/local/lib 中的庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!