c++动态链接库隐式链接 linux,Linux下c++经过动态连接库调用类

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

提示错误 undefined reference to `dlopen' 编译时增长“-ldl”选项便可解决。ios

提示错误 cannot open shared object file: No such file or directory 将当前目录的绝对路径添加到LD_LIBRARY_PATH便可 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/zhj/test_so/web

提示错误 undefined reference to 检查了一下,原来在makefile里少包含了一个文件。 这一类错误通常都是缺乏相应的类文件所致。ide

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

polygon.h //---------- //polygon.h: //---------- #ifndef POLYGON_H #define POLYGON_H #include orm

class polygon { protected: double side_length_;对象

public: polygon(); virtual ~polygon();ci

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试试吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值