c调用c++实现
参考:
- 文件说明
interface.h
/* file TestC.h */
#ifndef TESTC_H
#define TESTC_H
#ifdef __cplusplus
extern "C" {
#endif
//int add(int a, int b);
extern int add(int a, int b);
#ifdef __cplusplus
}
#endif
#endif /* TESTC_H */
interface.cpp
/* file TestC.cpp */
#ifndef _cplusplus
#define _cplusplus
#include "TestC.h"
#endif
int add(int a, int b)
{
return (a + b);
}
main.c
#include "stdio.h"
#include "TestC.h"
int main()
{
printf("add = %d\n", add(2, 5));
return 0;
}
2.调用关系
interface.cpp 实现一些功能,封装一部分c语言接口,成interface.h,然后由main.c 调用
3.如果编译时提示
undefined reference to “add”
关键在interface.cpp 中的
#ifndef _cplusplus
#define _cplusplus
#include "TestC.h"
#endif