Linux动态库的编译与使用 转载
http://hi..com/linuxlife/blog/item/0d3e302ae2384d3a5343c1b1.html
Linux下的动态库以.so为后缀,我也是初次在Linux下使用动态库,写一点入门步骤,以便以后能方便使用。
第一步:编写Linux程序库
文件1.动态库接口文件
//动态库接口文件getmaxlen.h
#ifndef _GETMAXLEN_H_
#define _GETMAXLEN_H_
int getMaxLen(int *sel,int N);
#endif
文件2.动态库程序实现文件
//动态库程序实现文件getmaxlen.c
#include "getmaxlen.h"
int getMaxLen(int *sel,int N)
{
int n1=1,n2=1;
for(int i=1;i<N;i++)
{
if(sel[i]>sel[i-1])
{
n2 ++;
if(n2 > n1)
{
n1 = n2;
}
}
else
{
n2 = 1;
}
}
return n1;
}
第二步:编译生成动态库
gcc getmaxlen.c –fPIC –shared –o libtest.so
由以上命令生成动态库libtest.so,为了不需要动态加载动态库,在命令时需以lib开头以.so为后缀。
–fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
–shared:指明编译成动态库。
第三步:使用动态库
1. 编译时使用动态库
文件1.动态库使用文件test.c
//使用动态库libtest.so,该文件名为test.c
#include "getmaxlen.h"
int main()
{
int Sel[] = {2,3,6,5,3,2,1,2,3,4,5,6,7,6,5};
int m;
m = getMaxLen(Sel,15);
printf("%d",m);
return 0;
}
编译命令:
gcc test.c –L . –l test –o test
–L:指明动态库所在的目录
-l:指明动态库的名称,该名称是处在头lib和后缀.so中的名称,如上动态库libtest.so的l参数为-l test。
测试:
ldd test
ldd 测试可执行文件所使用的动态库
2. 动态加载方式使用动态库
文件内容:
//动态库的动态加载使用
int main()
{
void *handle = NULL;
int (*getMaxLen)(int *sel,int N);
int sel[] = {1,2,5,4,5,8,6,5,9,5,4,5,4,1};
handle = dlopen("./libtest.so",RTLD_LAZY);
if(handle == NULL)
{
printf("dll loading error.\n");
return 0;
}
getMaxLen = (int(*)(int *,int))dlsym(handle,"getMaxLen");
if(dlerror()!=NULL)
{
printf("fun load error.\n");
return 0;
}
printf("%d\n",getMaxLen(sel,15));
}
编译命令:
gcc –ldl test1.c –o test
gcc -o test test.c ./libmytools.so
2. 如何编译动态库/静态库之编译Qt4.8.5静态库
1. 下载Qt 。需要注册一下账号!
a) 选择你需要的版本
3. 如何用gcc编译动态库
今天要用到静态库和动态库,于是写了几个例子来巩固一下基础。
hello1.c ————————————————————
#include <stdio.h>
void print1(int i) { int j; for(j=0;j<i;j++) { printf("%d * %d = %d\n",j,j,j*j); } }
hello2.c _________________________________________________
#include <stdio.h>
void print2(char *arr) { char c; int i=0; while((c=arr[i++])!='\0') { printf("%d****%c\n",i,c); } }
hello.c ____________________________________________________
void print1(int); void print2(char *);
int main(int argc,char **argv) { int i=100; char *arr="THIS IS LAYMU'S HOME!"; print1(i); print2(arr);
return 0; }
可以看到hello.c要用到hello1.c中的print1函数和hello2.c中的print2函数。所以可以把这两个函数组合为库,以供更多的程序作为组件来调用。
方法一:将hello1.c和hello2.c编译成静态链接库.a
[root@localhost main5]#gcc -c hello1.c hello2.c
//将hello1.c和hello2.c分别编译为hello1.o和hello2.o,其中-c选项意为只编译不链接。
[root@localhost main5]#ar -r libhello.a hello1.o hello2.o
//将hello1.o和hello2.o组合为libhello.a这个静态链接库
[root@localhost main5]#cp libhello.a /usr/lib
//将libhello.a拷贝到/usr/lib目录下,作为一个系统共享的静态链接库
[root@localhost main5]#gcc -o hello hello.c -lhello
//将hello.c编译为可执行程序hello,这个过程用到了-lhello选项,这个选项告诉gcc编译器到/usr/lib目录下去找libhello.a的静态链接库
以上的过程类似于windows下的lib静态链接库的编译及调用过程。
方法二:将hello1.o和hello2.o组合成动态链接库.so
[root@localhost main5]#gcc -c -fpic hello1.c hello2.c
//将hello1.c和hello2.c编译成hello1.o和hello2.o,-c意为只编译不链接,-fpic意为位置独立代码,指示编译程序生成的代码要适合共享库的内容这样的代码能够根据载入内存的位置计算内部地址。
[root@localhost main5]#gcc -shared hello1.o hello2.o -o hello.so
//将hello1.o和hello2.o组合为shared object,即动态链接库
[root@localhost main5]#cp hello.so /usr/lib
//将hello.so拷贝到/usr/lib目录下
[root@localhost main5]#gcc -o hello hello.c hello.so
//将hello.c编译链接为hello的可执行程序,这个过程用到了动态链接库hello.so
在这里要废话几句,其实一切的二进制信息都有其运作的机制,只要弄清楚了它的机制,并能够实现之,则任何此时此刻无法想象之事都将成为现实。当然,这两者之间的巨大鸿沟需要顶级的设计思想和顶级的代码来跨越。
4. Qt下如何编译库
akefile文件。一般是qt里自带的qmake工具。
首先先写好cpp和头文件,在当前目录下依次执行qmake -project,qmake,make即可编译。
另外,团IDC网上有许多产品团购,便宜有口碑
5. 怎么重新编译android 下面的动态库
使用动态库来编译动态库
A项目的android.mk文件如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := testa
LOCAL_SRC_FILES := testa.c
include $(BUILD_SHARED_LIBRARY)
生成的libtesta.so加入到E:\workspace\android-ndk-r8e\platforms\android-8\arch-arm\usr\lib\下面
项目B的文件目录结构如下:
jni
jni/jni/
jni/prebuilt/
jni目录下的mk文件如下:
include $(all-subdir-makefiles)
jni/prebuilt目录下的mk文件如下:
LOCAL_PATH := $(call my-dir)
#include $(CLEAR_VARS)
LOCAL_MODULE := libtesta
LOCAL_SRC_FILES := libtesta.so
include $(PREBUILT_SHARED_LIBRARY)
同时把libtesta.so也放入该目录下.
jni/jni目录下的mk文件内容:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -ltesta
LOCAL_MODULE := testb
LOCAL_SRC_FILES := testb.c
include $(BUILD_SHARED_LIBRARY)
这样生成libtestb.so文件, 同时eclipse在打包时会把libtesta.so, libtestb.so都加入到apk文件中,如果没有prebuilt那一步,那么在打包时会漏掉libtesta.so, 但编译会通过,因为编译读取的是编译系统的库文件目录(LOCAL_LDLIBS := -ltesta), 这点需要注意
java代码:
System.loadLibrary("testa");
System.loadLibrary("testb");
注意先后关系
6. 动态库链接编译
这里的动态的意思应该是模块代码是动态加载的
而不是随着应用程序一起编译
只要动态库里的函数接口不变
应用程序就无需重新编译
只需将动态库重新编译后替换掉旧的动态库即可
如果动态库的函数接口有变动
那么应用程序就要重新编译发布
这也是我的个人理解~~~