如何在c里调用c++的类函数
2012-08-22 09:54
387 查看
http://bbs.sjtu.edu.cn/bbscon?board=C&file=M.1300711242.A
在c++中定义了一个类,现在需要在c中调用这个类中的函数。
add.h
1
2 class Add{
3 public:
4 Add();
5 int x,y;
6 int sum();
7 };
8
9 extern "C"{
10 int call_add_sum_Cplus(void *pAdd);
11 void * constructor_add_Cplus();
12 void destructor_add_Cplus(void * pAdd);
13 }
add.cpp
1 #include "add.h"
2
3 Add::Add()
4 {
5 x=2;
6 y=3;
7 }
8
9 int Add::sum()
10 {
11 return x+y;
12 }
13
14 int call_add_sum_Cplus(void *pAdd)
15 {
16 return ((Add *)pAdd)->sum();
17 }
18
19
20 void * constructor_add_Cplus()
21 {
22 Add *p = new Add();
23 return (void *)p;
24 }
25
26 void destructor_add_Cplus(void * pAdd)
27 {
28 Add *p = (Add *)pAdd;
29 delete p;
30 }
31
32
add_c.h
1
2
3 int call_add_sum_C(void *pAdd);
4 void * constructor_add_C();
5 void destructor_add_C(void *pAdd);
6
add_c.c
1 #include "add_c.h"
2
3 int call_add_sum_Cplus(void *pAdd);
4 void * constructor_add_Cplus();
5 void destructor_add_Cplus(void * pAdd);
6
7 int call_add_sum_C(void *pAdd)
8 {
9 return call_add_sum_Cplus(pAdd);
10 }
11
12 void * constructor_add_C()
13 {
14 return constructor_add_Cplus();
15 }
16
17
18 void destructor_add_C(void *pAdd)
19 {
20 destructor_add_Cplus(pAdd);
21 }
main.c
1 #include <stdio.h>
2
3 void main(void)
4 {
5 void *p = constructor_add_C();
6 int a = call_add_sum_C(p);
7 printf("a = %d \n",a);
8 destructor_add_C(p);
9 }
10
Makefile
1 easy:main.o add.o add_c.o
2 gcc main.o add_c.o add.o -lstdc++ -o easy
3 main.o:main.c
4 gcc -c main.c -o main.o
5 add.o:add.cpp
6 g++ -c add.cpp -o add.o
7 add_c.o:add_c.c
8 gcc -c add_c.c -o add_c.o
9
10 clean:
11 rm *.o
结果
yang@ubuntu:~/Cincludeclass$ ls
add_c.c add_c.o add.h easy main.o
add_c.h add.cpp add.o main.c Makefile
yang@ubuntu:~/Cincludeclass$ ./easy
a = 5
在c++中定义了一个类,现在需要在c中调用这个类中的函数。
add.h
1
2 class Add{
3 public:
4 Add();
5 int x,y;
6 int sum();
7 };
8
9 extern "C"{
10 int call_add_sum_Cplus(void *pAdd);
11 void * constructor_add_Cplus();
12 void destructor_add_Cplus(void * pAdd);
13 }
add.cpp
1 #include "add.h"
2
3 Add::Add()
4 {
5 x=2;
6 y=3;
7 }
8
9 int Add::sum()
10 {
11 return x+y;
12 }
13
14 int call_add_sum_Cplus(void *pAdd)
15 {
16 return ((Add *)pAdd)->sum();
17 }
18
19
20 void * constructor_add_Cplus()
21 {
22 Add *p = new Add();
23 return (void *)p;
24 }
25
26 void destructor_add_Cplus(void * pAdd)
27 {
28 Add *p = (Add *)pAdd;
29 delete p;
30 }
31
32
add_c.h
1
2
3 int call_add_sum_C(void *pAdd);
4 void * constructor_add_C();
5 void destructor_add_C(void *pAdd);
6
add_c.c
1 #include "add_c.h"
2
3 int call_add_sum_Cplus(void *pAdd);
4 void * constructor_add_Cplus();
5 void destructor_add_Cplus(void * pAdd);
6
7 int call_add_sum_C(void *pAdd)
8 {
9 return call_add_sum_Cplus(pAdd);
10 }
11
12 void * constructor_add_C()
13 {
14 return constructor_add_Cplus();
15 }
16
17
18 void destructor_add_C(void *pAdd)
19 {
20 destructor_add_Cplus(pAdd);
21 }
main.c
1 #include <stdio.h>
2
3 void main(void)
4 {
5 void *p = constructor_add_C();
6 int a = call_add_sum_C(p);
7 printf("a = %d \n",a);
8 destructor_add_C(p);
9 }
10
Makefile
1 easy:main.o add.o add_c.o
2 gcc main.o add_c.o add.o -lstdc++ -o easy
3 main.o:main.c
4 gcc -c main.c -o main.o
5 add.o:add.cpp
6 g++ -c add.cpp -o add.o
7 add_c.o:add_c.c
8 gcc -c add_c.c -o add_c.o
9
10 clean:
11 rm *.o
结果
yang@ubuntu:~/Cincludeclass$ ls
add_c.c add_c.o add.h easy main.o
add_c.h add.cpp add.o main.c Makefile
yang@ubuntu:~/Cincludeclass$ ./easy
a = 5
相关文章推荐
- C++中如何调用C里面的函数
- C/C++: 函数调用, 栈的大小是如何被确定的
- c中如何打印函数调用堆栈? - C/C++ - ChinaUnix.net
- C++成员函数如何调用线程
- C中如何调用C++函数?
- 如何生成DLL文件并导出函数及如何在C++或C#中调用DLL中导出的函数
- c++中如何在主函数中调用其他文件内的函数?
- 如何在C++中创建可被Blueprint调用的全局函数
- C++如何调用JS函数
- c++中如何解决函数调用二维数组的问题。
- C中如何调用C++函数
- python嵌入C++------ boost.python如何在C++中调用含有不定长参数tuple变量和关键字参数dict变量的函数
- c++里的system()函数调用系统命令如何获取打印信息
- 在VC6.0中创建和使用静态链接库&amp;amp;&amp;amp;C如何调用C++ 成员函数
- C中如何调用C++函数?
- 如何调用C++函数?
- c++如何一个文件如何调用在同一个工程下的另一个文件的函数
- C中如何调用C++函数?
- C# 如何调用 C++ DLL中的函数接口和回调函数
- 理解 C++ 中继承层次的关键在于理解如何确定函数调用