http://hi.baidu.com/ablenavy/item/b498901c6826bbf587ad4e33

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里少包含了一个文件。 这一类错误一般都是缺少相应的类文件所致。

例子来源: http://www.linuxsir.org/bbs/printthread.php?t=266890

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

polygon.h //---------- //polygon.h: //---------- #ifndef POLYGON_H #define POLYGON_H #include <stdio.h>

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 <cmath>

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 <iostream> #include <dlfcn.h>

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

Linux下c++通过动态链接库调用类的更多相关文章

  1. linux下启动dbca或netmgr类的图形界面报错解决

    linux下启动dbca或netmgr类的图形界面报错解决    Xlib: connection to ":0.0" refused by server Xlib: No pro ...

  2. Linux下gcc编译生成动态链接库*.so文件并调用它【转载】

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...

  3. Linux下gcc编译生成动态链接库*.so文件并调用它 是转载的

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...

  4. Linux下gcc编译生成动态链接库*.so文件并调用它

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...

  5. Linux下gcc编译生成动态链接库*.so文件并调用它(注:执行Test程序后无需用export 命令指定.so库文件路径:方法在文中下方;)

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...

  6. LINUX下C语言编程调用函数、链接头文件以及库文件

    LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...

  7. Linux下C/C++代码调用PHP代码(转)

    Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char *  ...

  8. [转]linux 下 使用 c / c++ 调用curl库 做通信开发

    example:   1. http://curl.haxx.se/libcurl/c/example.html  2. http://www.libcurl.org/book:  1. http:/ ...

  9. .PHP后缀大写导致Linux下Composer找不到类

    在本地Windows写完一个Composer包,上传到Linux报错找不到类,纠结了一下午,最后发现是.PHP后缀大写导致的问题. mv Google2FA.PHP Google2FA.php

随机推荐

  1. NSS_09 gridpanel中的actioncolumn事件

    在设计角色权限时, 终于用到了grid的actioncolumn,如下: { header: '权限设定', xtype: 'actioncolumn', items: [{ icon: 'Conte ...

  2. Windows Server 2003 IIS支持ASP

    1.ASP支持是很简单的  这里简单的说一下 2.安装IIS服务   这里就不多演示了  很简单的 3.安装完成后打开IIS的扩展功能 ASP代码测试页为: <% response.write( ...

  3. 单元测试SimpleTest新手入门

    最近学习单元测试,先用了下PHPunit,结果安装问题一大堆,于是立刻放弃改试simpletest,感觉简单多了.下面列出步骤. 1.下载simpletest(版本1.1.0), http://www ...

  4. centos 减少tty数量的方法

    在linux中,包括本文介绍的centos系统中,tty系统默认是给出7个,前六个是terminal,一个用于X. 在centos5.x中减少tty数量,通过修改/etc/inittab来实现. [r ...

  5. XE5 ANDROID通过webservice访问操作MSSQL数据库

    上接XE5 ANDROID平台 调用 webservice 一.服务端 在ro里添加函数(在impl上添加阿东connection,adoquery,dataprovider) function TN ...

  6. cordova ios

    使用Cordova进行iOS开发 (环境配置及基本用法) 字数1426 阅读3044 评论0 喜欢5 安装Cordova CLI 1. cordova的安装: 1.1 安装cordova需要先安装no ...

  7. python杂记-5(装饰器)

    1.被装饰的函数有参数(一个参数): def w1(func): def inner(arg): # 验证1 # 验证2 # 验证3 return func(arg) return inner @w1 ...

  8. html 布局;css3+jq 下拉菜单;table分页动态添加行;html5本地存储;简单易用的html框架

    简单好用的html框架,预览图见最后: 源码: 1.页面布局使用table: table 嵌套 +iframe 布局: 2.下拉菜单为jq+css3 动画; css input 无边框,select下 ...

  9. Python之回调魔法

    Python中魔法(前后又下划线)会在对象的生命周期被回调. 借助这种回调, 可以实现AOP或者拦截器的思想. 在Python语言中提供了类似于C++的运算符重在功能:一下为Python运算符重在调用 ...

  10. access_ok()

    access_ok() 函数是用来代替老版本的 verify_area() 函数的.它的作用也是检查用户空间指针是否可用. 函数原型: access_ok (type, addr, size); 变量 ...