如何在linux下调用.so库里面的类成员函数?

muping2011 2012-05-04 04:08:03
//aa.h

class A //A类
{
char* B(char*b); //B成员函数
void C(char*c); //C成员函数

}

//aa.cpp
#include"aa.h"

char* A::B(char*b)
{
printf("b\n");
}

void A::C(char *c)
{
printf("c\n");
}


生成.so文件

g++ aa.cpp -fPIC -shared -o aa.so

怎么在main.cpp中调用aa.so中的B和C函数?

//main.cpp

#include <stdio.h>
#include <dlfcn.h>
main()
{
//如何才能调用B和C函数?
}
...全文
402 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
muping2011 2012-05-07
  • 打赏
  • 举报
回复
[User:root Time:16:47:29 Path:/home/liangdong/cpp]$ g++ -o main main.cpp -L. -ltest

———————————————————————————————————————————————————

请问这里的这个-L -ltest参数是什么意思?
qq120848369 2012-05-04
  • 打赏
  • 举报
回复
[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ ll
total 12
-rw-r--r--. 1 root root 120 May 4 16:46 libtest.cpp
-rw-r--r--. 1 root root 85 May 4 16:45 libtest.h
-rw-r--r--. 1 liangdong daemon 141 May 4 16:42 main.cpp
[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ cat libtest.cpp libtest.h main.cpp
#include "libtest.h"
#include <iostream>
using namespace std;

void test::function() {
cout<<"test::function"<<endl;
}
#ifndef _H_TEST
#define _H_TEST

class test {
public:
void function();
};

#endif
#include <iostream>
#include "libtest.h"
using namespace std;

int main(int argc, char* const argv[]) {
test t;
t.function();
return 0;
}
[User:root Time:16:46:55 Path:/home/liangdong/cpp]$ g++ -o libtest.so -fPIC -shared libtest.cpp -I.
[User:root Time:16:47:29 Path:/home/liangdong/cpp]$ g++ -o main main.cpp -L. -ltest
[User:root Time:16:47:48 Path:/home/liangdong/cpp]$ ll
total 28
-rw-r--r--. 1 root root 120 May 4 16:46 libtest.cpp
-rw-r--r--. 1 root root 85 May 4 16:45 libtest.h
-rwxr-xr-x. 1 root root 5773 May 4 16:47 libtest.so
-rwxr-xr-x. 1 root root 5876 May 4 16:47 main
-rw-r--r--. 1 liangdong daemon 141 May 4 16:42 main.cpp
[User:root Time:16:47:50 Path:/home/liangdong/cpp]$ ./main
./main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
[User:root Time:16:47:51 Path:/home/liangdong/cpp]$ export LD_LIBRARY_PATH=.
[User:root Time:16:48:01 Path:/home/liangdong/cpp]$ ./main
test::function
[User:root Time:16:48:04 Path:/home/liangdong/cpp]$
muping2011 2012-05-04
  • 打赏
  • 举报
回复
求代码 求代码 求代码

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧