首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么在linux下调用.so库里面的类成员函数

2012-05-09 
如何在linux下调用.so库里面的类成员函数?//aa.hclass A//A类{char* B(char*b)//B成员函数void C(char*c)

如何在linux下调用.so库里面的类成员函数?
//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函数?
}

[解决办法]

C/C++ code
[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ lltotal 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_TESTclass 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]$ lltotal 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]$ ./maintest::function[User:root Time:16:48:04 Path:/home/liangdong/cpp]$ 

热点排行