LInux下动态链接库的生成

来源:互联网 发布:python 二维数组赋值 编辑:程序博客网 时间:2024/05/21 09:30

参考http://blog.chinaunix.net/uid-20801390-id-3157134.html

首先是动态调用动态库


test_so1.h和test_so1.cpp生成test_so1.so动态库。

test_so2.h和test_so2.cpp生成test_so2.so动态库。

test_dll.cpp生成test_dll可执行程序,test_dl通过dlopen系列等API函数,并使用函数指针以到达动态调用不同so库中test函数的目的。

////////////////////////////////test_so1.h//////////////////////////////////////////////////////

#include<iostream>

extern "C" {

int test(void);

}

 

////////////////////////////////test_so1.cpp//////////////////////////////////////////////////////

#include "test_so1.h"

int test(void)

{

        cout<<"USING TEST_SO1.SO NOW!\n"<<endl;//注意此处与test_so2.cpp中的

                                                                        //test函数的不同

return 0;

}

 

//////////////////////////////// test_so2.h //////////////////////////////////////////////////////

#include<iostream>

extern "C" {

int test(void);

}

 

////////////////////////////////test_so2.cpp//////////////////////////////////////////////////////

#include "test_so2.h"

int test(void)

{

        cout<<"USING TEST_SO2.SO NOW!\n"<<endl;//注意此处与test_so1.cpp中的

                                                                        //test函数的不同

        return 0;

}

 

////////////////////////////////test_dll.cpp//////////////////////////////////////////////////////

#include<iostream>

#include<dlfcn.h>

#include<stdio.h>

#include<stdlib.h>

 

int main(int argc, char **argv)

{

        if(argc!=2)

        {

                cout<<"Argument Error! You must enter like this:"<<endl;

                cout<<"./test_dl test_so1.so"endl;

                exit(1);

        }

 

        void *handle;

        char *error;

        typedef void (*pf_t)();   //声明函数指针类型

 

        handle = dlopen (argv[1], RTLD_NOW);     //打开argv[1]指定的动态库

 

        if (!handle)

        {

                fprintf (stderr, "%s\n", dlerror());

                exit(1);

        }

 

        dlerror();   

         pf_t pf=(pf_t)dlsym(handle,"test" );    //指针pf指向test在当前内存中的地址

        if ((error = dlerror()) != NULL) 

        {

                fprintf (stderr, "%s\n", error);

                exit(1);

        }

        pf();        //通过指针pf的调用来调用动态库中的test函数

        dlclose(handle);      //关闭调用动态库句柄

        return 0;

}


//然后下面是参考的文章中的Makefile

////////////////////////////////makefile//////////////////////////////////////////////////////

.SUFFIXES: .c .cpp .o

CC=g++  -shared -fPIC

GCC=g++

 

all:test_so1.so test_so2.so test_dll clean

 

OBJ1=test_so1.o

OBJ2=test_so2.o

OBJ3=test_dll.o

 

test_so1.so:$(OBJ1)

        $(CC) -o $@ $?

        cp $@ /usr/lib

 

test_so2.so:$(OBJ2)

        $(CC) -o $@ $?

        cp $@ /usr/lib

 

test_dll:$(OBJ3)

        $(GCC)  -o $@ $? -ldl

 

.cpp.o:

        $(CC) -c $*.cpp

.c.o:

        $(CC) -c $*.c

clean:

        rm -f *.o

在终端中的当前指令下执行make是,生成了test_so1.so和test_so2.so的动态库文件,但是还会出现下面的错误。


huangbo@huangbo:~/test4$ sudo make
g++  -shared -fPIC -c test_so2.cpp
g++  -shared -fPIC -o test_so2.so test_so2.o
cp test_so2.so /usr/lib
g++     test_dll.cpp   -o test_dll
/tmp/cc1ywB9U.o:在函数‘main’中:
test_dll.cpp:(.text+0x6b):对‘dlopen’未定义的引用
test_dll.cpp:(.text+0x7b):对‘dlerror’未定义的引用
test_dll.cpp:(.text+0xa6):对‘dlerror’未定义的引用
test_dll.cpp:(.text+0xb7):对‘dlsym’未定义的引用
test_dll.cpp:(.text+0xc0):对‘dlerror’未定义的引用
test_dll.cpp:(.text+0x109):对‘dlclose’未定义的引用
collect2: error: ld returned 1 exit status
make: *** [test_dll] 错误 1

由于自己是菜鸟,还不知道是什么原因引起的。经查资料发现。如果执行

huangbo@huangbo:~/test4$ g++ test_dll.cpp -ldl -o test_dll

则可以生成test_dll的动态库。

然后则可以正常运行                                                                 

huangbo@huangbo:~/test4$ ./test_dll test_so1.so
USING TEST_SO1.SO NOW!
huangbo@huangbo:~/test4$ ./test_dll test_so2.so
USING TEST_SO2.SO NOW!
huangbo@huangbo:~/test4$ ./test_dll
Argument Error! You must enter like this:
./test_dl test_so1.so

参考的文章是http://www.linuxdiyf.com/linux/16754.html。原因可以看这篇文章  ////////ps:上面的Makefile可以正常生成可执行文件,是我弄错了导致出现问题!!

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕前没有吃叶酸怎么办 严重少精和畸形怎么办 大学混了四年怎么办 和家人相处很累怎么办 40多岁眼睛花怎么办 侧方停车右边宽怎么办 有公主病的老婆怎么办? 圆通快递太慢了怎么办 美台军舰互停怎么办 安装包删除不掉怎么办 艾灸后皮肤极痒怎么办 拔罐如果有水泡怎么办 拔罐拔出水泡来怎么办 拔罐拔出了水泡怎么办 风湿引起的背疼怎么办 拔罐减肥不瘦怎么办 艾灸后脸上长痘怎么办 艾灸烟大怎么办 湿毛巾 月经推迟一个月了还不来怎么办 埋线了喝酒了怎么办 对待孩子脾气暴燥怎么办 买的新鞋子臭脚怎么办 鞋子臭脚怎么办如何除 惠普803墨盒干了怎么办 酷派手机无命令怎么办 华为手机锁机了怎么办 小孩被蜘蛛咬了怎么办 我是一个不爱说话的人怎么办 光动能手表停了怎么办 电波表收不到波怎么办 吃了壮阳药头疼怎么办 吃了伟哥后头痛怎么办 伟哥吃了不管用怎么办 钱站的钱还不起怎么办 面试工资要低了怎么办 线切割环保来查怎么办 喝白酒咽不下去怎么办 翠佛堂的玉假了怎么办 300英雄账号忘了怎么办 买了假酒不承认怎么办 买到假酒不承认怎么办