您的位置:首页 > 运维架构 > Linux

Linux动态库的编译与使用

2014-04-01 13:23 253 查看
http://hi.baidu.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

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \

    rtm_map.o iptunnel.o ip6tunnel.o tunnel.o ipneigh.o ipntable.o iplink.o \

    ipmaddr.o ipmonitor.o ipmroute.o ipprefix.o iptuntap.o iptoken.o \

    ipxfrm.o xfrm_state.o xfrm_policy.o xfrm_monitor.o \

    iplink_vlan.o link_veth.o link_gre.o iplink_can.o \

    iplink_macvlan.o iplink_macvtap.o ipl2tp.o link_vti.o \

    iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \

    link_iptnl.o link_gre6.o

TARGET  := libip.so

CC      := gcc

LIBS    := ../lib/libnetlink.a ../lib/libutil.a -ldl

LDFLAGS :=  -Wl,-export-dynamic

DEFINES := -D_GNU_SOURCE -DRESOLVE_HOSTNAMES -D_GNU_SOURCE

INCLUDE := -I.

CCOPTS = -O2

WFLAGS := -Wall -Wstrict-prototypes  -Wmissing-prototypes

WFLAGS += -Wmissing-declarations -Wold-style-definition

CFLAGS  := -g $(WFLAGS) $(CCOPTS) -I../include $(DEFINES) $(INCLUDE)

CXXFLAGS:= $(CFLAGS)

SHARE   := -fPIC -shared -o

.PHONY : everything objs clean veryclean rebuild

everything : $(TARGET)

all : $(TARGET)

objs : $(IPOBJ)

rebuild: veryclean everything

clean :

 rm -fr *.o

veryclean : clean

 rm -fr $(TARGET)

$(TARGET) : $(IPOBJ) 

 $(CC) $(CXXFLAGS) $(SHARE) $@ $(IPOBJ) $(LDFLAGS) $(LIBS)

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: