linuxc++动态链接库so编写

linuxc++动态链接库so编写
Linux下的动态链接库是.so⽂件,即:Shared Object,下⾯是⼀个简单的例⼦说明如何写.so以及程序如何动态载⼊.so中的函数对象
testso.h:
#ifndef _TESTSO_H
#define _TESTSO_H
extern "C" {
int myadd(int a, int b);
typedef int myadd_t(int, int); // myadd function type
}
#endif // _TESTSO_H
testso.cpp:
#include "testso.h"
extern "C"
int myadd(int a, int  b)
{
return a + b;
}
编译so:
g++  -shared  -fPIC  -o testso.so testso.cpp小撒探会
注意,-shared参数和-fPIC参数⾮常重要:-shared 告诉gcc要⽣成的是动态链接库;-fPIC 告诉gcc⽣成的⽣成的代码是⾮位置依赖的,⽅⾯的⽤于动态链接。
开普勒第三定律在主程序⾥调⽤这个动态链接库:
main.cpp:
#include
#include
// for dynamic library函数
#include "testso.h"
void print_usage(void)
{
printf("Usage: main SO_PATH/n");
}
saiboint main(int argc, char *argv[])
{
if (2 != argc) {
print_usage();
exit(0);
}
const char *soname = argv[1];
void *so_handle = dlopen(soname, RTLD_LAZY); // 载⼊.so⽂件
if (!so_handle) {
fprintf(stderr, "Error: load so `%s' failed./n", soname);
exit(-1);
}
dlerror(); // 清空错误信息
myadd_t *fn = (myadd_t*)dlsym(so_handle, "myadd"); // 载⼊函数
char *err = dlerror();
if (NULL != err) {
fprintf(stderr, "%s/n", err);
exit(-1);
}
printf("myadd 57 + 3 = %d/n", fn(57, 3)); // 调⽤函数
dlclose(so_handle); // 关闭so句柄
return 0;
}
编译主程序:
g++ main.cpp -o main -ldl
注意:要加上-ldl
好了,上⾯就是如何写和调⽤动态库中的C函数。
对于C++中的类,不能直接导出,需要通过继承的⽅式才能从动态链接库中导出:=====================
testso.h:
#ifndef _TESTSO_H
#define _TESTSO_H
// 只能通过基类调⽤,因此需要先定义⼀个基类,然后在create中⽣成真正需要⽣成的对象。class Base
{
public:
int a, b;
virtual int add(void)
{
return -1;
}
};
class A : public Base
{
public:
int add(void);
};
extern "C" {
Base* create(void);
void destroy(Base *p);
typedef Base* create_t(void);  // create factory
typedef void destory_t(Base*); // destory
}
#endif // _TESTSO_H
testso.cpp:
#include "testso.h"
int A::add(void)
{
return a + b;
}
extern "C"
{
Base* create(void) // 注意:create函数必须返回Base的对象,不能直接返回A的
//      对象,否则后⾯调⽤A::add()的时候会提⽰错误。
{
return new A;
}
void destory(Base *p)
{
if (p) delete p;
}
}
main.cpp: // 这⾥需要注意
#include
#include
#include
#include "testso.h"
void print_usage(void)
{
printf("Usage: myso SO_PATH/n");
}
int main(int argc, char *argv[])
{
if (2 != argc) {
print_usage();
exit(0);
}
u20战场const char *soname = argv[1];
void *so_handle = dlopen(soname, RTLD_LAZY);
if (!so_handle) {
fprintf(stderr, "Error: load so `%s' failed./n", soname);
exit(-1);
涉日
}
dlerror();
create_t *create = (create_t*) dlsym(so_handle, "create");
if (NULL != err) {
fprintf(stderr, "%s/n", err);
exit(-1);
}
Base *pa = create();
pa->a = 57;
pa->b = 3;
printf("A.add(57, 3)=%d/n", pa->add()); // 注意,这⾥通过虚函数实现了                                            // 对A::add()的调⽤。
destory_t *destory = (destory_t*) dlsym(so_handle, "destory");
if (NULL != err) {
fprintf(stderr, "%s/n", err);
exit(-1);
}
destory(pa);
pa = NULL;
dlclose(so_handle);
printf("DONE!/n");
ifs系统return 0;
}

本文发布于:2024-09-21 18:55:22,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/671820.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:需要   函数   主程序   对象   动态   注意
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议