Linux下c++通过动态链接库调用类
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++通过动态链接库调用类的更多相关文章
- linux下启动dbca或netmgr类的图形界面报错解决
linux下启动dbca或netmgr类的图形界面报错解决 Xlib: connection to ":0.0" refused by server Xlib: No pro ...
- Linux下gcc编译生成动态链接库*.so文件并调用它【转载】
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...
- Linux下gcc编译生成动态链接库*.so文件并调用它 是转载的
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...
- Linux下gcc编译生成动态链接库*.so文件并调用它
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...
- Linux下gcc编译生成动态链接库*.so文件并调用它(注:执行Test程序后无需用export 命令指定.so库文件路径:方法在文中下方;)
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...
- LINUX下C语言编程调用函数、链接头文件以及库文件
LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...
- Linux下C/C++代码调用PHP代码(转)
Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char * ...
- [转]linux 下 使用 c / c++ 调用curl库 做通信开发
example: 1. http://curl.haxx.se/libcurl/c/example.html 2. http://www.libcurl.org/book: 1. http:/ ...
- .PHP后缀大写导致Linux下Composer找不到类
在本地Windows写完一个Composer包,上传到Linux报错找不到类,纠结了一下午,最后发现是.PHP后缀大写导致的问题. mv Google2FA.PHP Google2FA.php
随机推荐
- Spark RDD整理
参考资料: Spark和RDD模型研究:http://itindex.net/detail/51871-spark-rdd-模型 理解Spark的核心RDD:http://www.infoq.com/ ...
- Andriod docs加载速度慢的问题解决
网上找了个类, import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import ja ...
- TortoiseGit 安装和使用的图文教程
TortoiseGit.SourceTree都是Windows下不错的Git客户端工具,下面介绍一下TortoiseGit安装和使用的方法. 安装TortoiseGit并使用它需要两个软件:Torto ...
- 简单的下拉刷新以及优化--SwipeRefreshLayout
代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...
- highchars
var drawChart = function(sourceUrl) { $.ajax({ "type" : "post", "url" ...
- 基于Python的密码生成程序的优化
近期刚刚组织完内部的Python基础培训.GUI的开发培训,之后布置的作业是两人一组,利用前面所写的一些模块做一些小软件. 具体就是模拟Advanced Password Generator这个软件的 ...
- Win8.1 IIS6 SQL SERVER 2012 执行 SqlServices.InstallSessionState 出错
新装了WIN8.1,感觉很不错. 新建了第一个站点是,在执行 SqlServices.InstallSessionState("localhost", null, SessionS ...
- 解决sublime text 2总是在新窗口中打开文件
在mac下不是很喜欢sublime text 2 总是在新窗口中打开文件,很麻烦,文件打多了,就会出现N多窗口,虽然可以直接打开当前目录可以解决,但有时候查看其它项目中的单个文件,就比较麻烦.百度一直 ...
- NGUI3.5系列教程之 UILabel
此NGUI版本为:3.5.1 NGUI 的UILabel脚本下的文字框可以用BBCode设置:[b]Bold[/b] 粗体 [i]italic[/i] 斜体 [u]underline[/u]下划线 [ ...
- ASP.NET MVC 学习第二天
今天使用mvc完成简单的增删改,内容比较简单,来熟悉一下mvc,数据库操作是用前面的ef,也算是温习一下ef吧. 新建mvc项目,在项目中的Models内添加ef,我这里只操作一下简单的user表.里 ...