惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
博客园 - 叶小钗
博客园 - 司徒正美
Recent Announcements
Recent Announcements
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
罗磊的独立博客
TaoSecurity Blog
TaoSecurity Blog
D
Docker
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
T
Tor Project blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
S
Schneier on Security
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
云风的 BLOG
云风的 BLOG
Latest news
Latest news
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
Cyberwarzone
Cyberwarzone
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Webroot Blog
Webroot Blog
L
LINUX DO - 最新话题
B
Blog RSS Feed
GbyAI
GbyAI
I
InfoQ
V
Vulnerabilities – Threatpost

博客园 - draeag

环境变量设置 linux下获取时间差 gcc / g++ 编译选项 常用软件 MAC IP等相关 union内存分配方案 颜色表示 我看各平台的界面呈现 常用开发工具 委托与事件 [转载]HTTP协议详解 Get和Post方法的区别 Windows DNA .NET 运行原理之CLR WPF之长短 .NET Framework/CLR之长短 COM, DCOM, COM+ , DTC, MSMQ,.NET Remoting,WCF COM线程模型概要 通信相关类
Linux下c++通过动态链接库调用类
draeag · 2013-08-15 · via 博客园 - draeag

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