Linux动态链接库文件的扩展名是,Linux下c++通过动态链接库调用类(转)

我的程序是一个类,在网上找了半天,都是c的例子,c++的类封装成静态库倒容易,可是如何封装成动态库,在其它程序中调用呢?

Linux下的动态链接库叫so,即Shared Object,共享对象。一些函数就不说了,网上多的是。把我遇到的问题写下来吧

提示错误 undefined reference to `dlopen'

编译时增加“-ldl”选项即可解决。

提示错误 cannot open shared object file: No such file or directory

将当前目录的绝对路径添加到LD_LIBRARY_PATH即可

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/zhj/test_so/

提示错误 undefined reference to

检查了一下,原来在makefile里少包含了一个文件。

这一类错误一般都是缺少相应的类文件所致。

例子来源:

这个扩展名为hpp的文件,我分解了一下,下面就上代码吧

polygon.h

//----------

//polygon.h:

//----------

#ifndef POLYGON_H

#define POLYGON_H

#include

class polygon {

protected:

double side_length_;

public:

polygon();

virtual ~polygon();

void set_side_length(double side_length);

virtual double area() const;

};

// the types of the class factories

typedef polygon* create_t();

typedef void destroy_t(polygon*);

#endif

polygon.cpp

//----------

//polygon.cpp:

//----------

#include "polygon.h"

polygon::polygon()

{

//set_side_length(0);

}

polygon::~polygon()

{

}

void polygon::set_side_length(double side_length)

{

printf("polygon side_length: %f\n\n",side_length);

side_length_ = side_length;

printf("polygon side_length_: %f\n\n",side_length_);

}

double polygon::area() const

{

return 0;

}

triangle.cpp

//----------

//triangle.cpp:

//----------

#include "polygon.h"

#include

class triangle : public polygon {

public:

virtual double area() const {

printf("triangle side_length_: %f\n\n",side_length_);

return side_length_ * side_length_ * sqrt(3) / 2;

}

};

// the class factories

extern "C" polygon* create() {

return new triangle;

}

extern "C" void destroy(polygon* p) {

delete p;

}

main.cpp

//----------

//main.cpp:

//----------

#include "polygon.h"

#include #include

int main() {

using std::cout;

using std::cerr;

// load the triangle library

void* triangle = dlopen("./triangle.so", RTLD_LAZY);

if (!triangle) {

cerr << "Cannot load library: " << dlerror() << '\n';

return 1;

}

// reset errors

dlerror();

// load the symbols

create_t* create_triangle = (create_t*) dlsym(triangle, "create");

const char* dlsym_error = dlerror();

if (dlsym_error) {

cerr << "Cannot load symbol create: " << dlsym_error << '\n';

return 1;

}

destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");

dlsym_error = dlerror();

if (dlsym_error) {

cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';

return 1;

}

// create an instance of the class

polygon* poly = create_triangle();

// use the class

poly->set_side_length(7);

cout << "The area is: " << poly->area() << '\n';

// destroy the class

destroy_triangle(poly);

// unload the triangle library

dlclose(triangle);

}

makefile

objects = main.o polygon.o

main: $(objects)

g++ -g -rdynamic -o $@ $(objects) -ldl

main.o: main.cpp

g++ -g -c main.cpp

polygon.o: polygon.cpp polygon.h

g++ -g -c polygon.cpp

.PHONY : clean

clean :

-rm main $(objects)

用下面的命令编译,生成libtriangle.so

g++ -g -fpic -shared -o libtriangle.so triangle.cpp polygon.cpp

然后make一下,运行main试试吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值