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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - draeag

环境变量设置 linux下获取时间差 gcc / g++ 编译选项 常用软件 MAC IP等相关 union内存分配方案 颜色表示 我看各平台的界面呈现 - draeag 常用开发工具 委托与事件 [转载]HTTP协议详解 Get和Post方法的区别 Windows DNA .NET 运行原理之CLR WPF之长短 .NET Framework/CLR之长短 - draeag 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试试吧!