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