linux 下共享库创建及使用
1.創建共享庫
[xpan@localhost 2.5.3]$ ls
libhello.c? libhello.h? usehello.c
[xpan@localhost 2.5.3]$ gcc -fPIC -Wall -g -c libhello.c
參數說明:
-fPIC 參數生成與位置無關的代碼;
-W 或 -Wall 去除所有警告;
-g? 指加上調試信息;
-c 是編譯之意;
[xpan@localhost 2.5.3]$ ls libhello.o
libhello.o
[xpan@localhost 2.5.3]$ gcc -shared -o libhello.so.1.0 libhello.o
[xpan@localhost 2.5.3]$ ls
libhello.c? libhello.h? libhello.o? libhello.so.1.0? usehello.c
[xpan@localhost 2.5.3]$ ln -sf libhello.so.1.0 libhello.so
[xpan@localhost 2.5.3]$ ls
libhello.c? libhello.h? libhello.o? libhello.so? libhello.so.1.0? usehello.c
[xpan@localhost 2.5.3]$ ls libhello.so -l
lrwxrwxrwx 1 xpan xpan 15 6月? 11 09:36 libhello.so -> libhello.so.1.0
[xpan@localhost 2.5.3]$
2.使用共享庫
[xpan@localhost 2.5.3]$ ls
libhello.c? libhello.h? libhello.o? libhello.so? libhello.so.1.0? usehello.c
[xpan@localhost 2.5.3]$ cat libhello.h
#ifndef _libhello_H_
#define _libhello_H_
void print_hello(void);
#endif /*_libhello_H_*/
[xpan@localhost 2.5.3]$ cat usehello.c
#include "libhello.h"
/*hello*/
int main(void)
{
? print_hello();
? return 0;
}
[xpan@localhost 2.5.3]$ gcc -Wall -g -c usehello.c -o usehello.o???? //編譯
[xpan@localhost 2.5.3]$ ls
libhello.c? libhello.o?? libhello.so.1.0? usehello.o
libhello.h? libhello.so? usehello.c
[xpan@localhost 2.5.3]$ gcc -g -o usehello_dynamic usehello.o -L ./ -lhello //連接引用
[xpan@localhost 2.5.3]$ ls
libhello.c? libhello.o?? libhello.so.1.0? usehello_dynamic
libhello.h? libhello.so? usehello.c?????? usehello.o
[xpan@localhost 2.5.3]$ ldd usehello_dynamic
?? ?linux-gate.so.1 =>? (0xb7726000)
?? ?libhello.so => not found
?? ?libc.so.6 => /lib/libc.so.6 (0x4c645000)
?? ?/lib/ld-linux.so.2 (0x4c622000)
[xpan@localhost 2.5.3]$ LD_LIBRARY_PATH=$(pwd) ./usehello_dynamic
hello world ,this is library
指定庫的搜索路徑
1. LD_LIBRARY_PATH=$(pwd)
2. 拷貝此庫"libhello.so"到/usr/lib 或 /lib 文件夾中,或者在此兩個文件夾任意一個中創建一個到該庫的快捷方式 ,并命名為libname.so;
3. 在"/etc/ld.so.conf"中,每一行添加一個路經,完成以上設置后再運行ldconfig命令更新信息,加入的目錄下的所有庫文件都可見。當靜態庫和動態庫同名時, gcc命令將優先使用動態庫。
?
?
?
?
轉載于:https://www.cnblogs.com/panxihua/archive/2012/06/11/2544280.html
總結
以上是生活随笔為你收集整理的linux 下共享库创建及使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EditorGridPanel的网格样式
- 下一篇: LAUNCHPAD运行5110测试程序