歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下共享庫的系統路徑和查找

Linux下共享庫的系統路徑和查找

日期:2017/2/28 14:54:57   编辑:Linux教程

在使用qvfb做framebuffer模擬的時候,發現幾個問題:第一個就是Ubuntu的確不專業,沒有很多庫的支持,最好使用RedHat,Fedora之類的發行版本,這樣會少很多編譯之類的麻煩.

由於在Ubuntu下編譯qvfb缺少了很多庫文件,而在沒有網絡的情況下,補齊這些庫和頭文件是非常麻煩的,所以准備在fedora中編譯好後再放到Ubuntu中使用.由於Ubuntu和fedora下qtsdk的安裝不一致,從而發現fedora下編譯好的qvfb放到Ubuntu下執行,發現找不到庫.

利用strace工具看一下這個過程,因為是在fedora下編譯的,所以makefile中的鏈接選項有指明鏈接庫的地址-rpath,這個地址就是sdk安裝目錄下的庫目錄.

所以在fedora下直接打開這個目錄,加載相應的庫文件.而在Ubuntu下sdk的安裝目錄不一致,系統的庫搜索路徑又沒有這個庫,所以執行肯定會報錯.在Ubuntu下strace下這個過程,就會發現有很多open /usr/lib之類的操作.

man ld,該文檔中給出了共享庫的系統路徑和查找優先級:

The linker uses the following search paths to locate required

shared libraries:

1. Any directories specified by -rpath-link options.

2. Any directories specified by -rpath options. The difference

between -rpath and -rpath-link is that directories specified by

-rpath options are included in the executable and used at

runtime, whereas the -rpath-link option is only effective at

link time. Searching -rpath in this way is only supported by

native linkers and cross linkers which have been configured

with the --with-sysroot option.

3. On an ELF system, for native linkers, if the -rpath and

-rpath-link options were not used, search the contents of the

environment variable "LD_RUN_PATH".

4. On SunOS, if the -rpath option was not used, search any

directories specified using -L options.

5. For a native linker, the search the contents of the environment

variable "LD_LIBRARY_PATH".

6. For a native ELF linker, the directories in "DT_RUNPATH" or

"DT_RPATH" of a shared library are searched for shared

libraries needed by it. The "DT_RPATH" entries are ignored if

"DT_RUNPATH" entries exist.

7. The default directories, normally /lib and /usr/lib.

8. For a native linker on an ELF system, if the file

/etc/ld.so.conf exists, the list of directories found in that

file.

If the required shared library is not found, the linker will issue

a warning and continue with the link.

所以,如果要想在ubuntu下運行fedora下編譯的qvfb,可以在/usr/lib下做一下qt庫的鏈接,將其指向qtsdk安裝目錄的庫目錄下的庫.這樣,就可以運行了.其實在嵌入式qt開發中也是這樣的:所有的動態庫我們都放在/lib下,這樣應用程序就都可以找到庫了。

相關閱讀:關於Ubuntu添加共享庫路徑 http://www.linuxidc.com/Linux/2011-10/45745.htm

Copyright © Linux教程網 All Rights Reserved