c调用c++实现
c调用c++实现
参考:
- 文件说明
interface.h
/* file TestC.h */
#ifndefTESTC_H
#defineTESTC_H
#ifdef__cplusplus
extern"C"{
#endif
//int add(int a, int b);
externintadd(inta,intb);
#ifdef__cplusplus
}
#endif
#endif/* TESTC_H */
interface.cpp
/* file TestC.cpp */
#ifndef_cplusplus
#define_cplusplus
#include"TestC.h"
#endif
intadd(inta,intb)
{
return(a+b);
}
main.c
#include"stdio.h"
#include"TestC.h"
intmain()
{
printf("add = %d\n",add(2,5));
return0;
}
2.调用关系
interface.cpp 实现一些功能,封装一部分c语言接口,成interface.h,然后由main.c 调用
3.如果编译时提示
undefined reference to "add"
关键在interface.cpp 中的
#ifndef_cplusplus
#define_cplusplus
#include"TestC.h"
#endif
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。