C++程序调用C函数
来源:互联网 发布:最好用安卓日历软件 编辑:程序博客网 时间:2024/06/03 21:58
这种需求很多,又因为C++和C是两种完全不同的编译链接处理方式,所以要稍加处理.总结大致有两大类实现方法.
文中给出的是完整的,具体的,但又最基本最简单的实现,至于理论性的东西在网上很容易搜索的到.
一.通过处理被调用的C头文件
a.h:
#ifndef __A_H
#define __A_H
#ifdef __cplusplus
extern "C" {
#endif
int ThisIsTest(int a, int b);
#ifdef __cplusplus
}
#endif
#endif
a.c:
#include "a.h"
int ThisIsTest(int a, int b) {
return (a + b);
}
aa.h:
class AA {
public:
int bar(int a, int b);
};
aa.cpp:
#include "a.h"
#include "aa.h"
#include "stdio.h"
int AA::bar(int a, int b){
printf("result=%d/n", ThisIsTest(a, b));
return 0;
}
main.cpp:
#include "aa.h"
int main(int argc, char **argv){
int a = 1;
int b = 2;
AA* aa = new AA();
aa->bar(a, b);
delete(aa);
return(0);
}
Makefile:
all:
gcc -Wall -c a.c -o a.o
gcc -Wall -c aa.cpp -o aa.o
gcc -Wall -c main.cpp -o main.o
g++ -o test *.o
二. 通过处理调用的C++文件
恢复a.h文件为一般性C头文件,在aa.cpp文件中extern包含a.h头文件或函数.
a.h:
#ifndef __A_H
#define __A_H
int ThisIsTest(int a, int b);
#endif
aa.cpp:
extern "C"
{
#include "a.h"
}
#include "aa.h"
#include "stdio.h"
int AA::bar(int a, int b){
printf("result=%d/n", ThisIsTest(a, b));
return 0;
}
or
aa.cpp:
#include "aa.h"
#include "stdio.h"
extern "C"
{
int ThisIsTest(int a, int b);
}
int AA::bar(int a, int b){
printf("result=%d/n", ThisIsTest(a, b));
return 0;
}
- C++程序调用C函数
- C程序调用C++函数
- C++程序调用C函数
- c++程序调用c函数
- c程序调用汇编函数
- 汇编函数调用c程序
- C程序中如何调用C++函数
- 浅谈c程序函数调用过程
- c程序调用nasm汇编函数
- C++程序中如何调用C函数
- 在C程序中调用汇编函数
- 汇编程序调用c程序
- c调用外部程序
- Matlab调用c程序
- java调用c程序
- MATLAB调用C程序
- Matlab调用C程序
- matlab调用c程序
- PowerDesigner(CDM—PDM—SQL脚本的转换流程) 随笔
- SCJP试题五
- java keywords and language tutorial
- Advanced Serial Programming(翻译)
- showModalDialog和showModelessDialog
- C++程序调用C函数
- 在Symfony中直接运用SQL语句检索数据
- TCL中的正则表达式
- Python发送邮件的类
- 祝你高飞:功德圆满的实习生们~
- Linux后台运行命令详述
- lamp性能调优
- eclipse 断点调试快捷键
- PHP函数 Join() 的用法