一.C调用C++
main.c
#include <stdio.h>
#include "Test1.hpp"
int main(int argc, const char * argv[]) {
printf("%d\n",b());
return 0;
}
Test1.hpp
#ifndef Test1_hpp
#define Test1_hpp
#include <stdio.h>
#ifdef __cplusplus
extern "C"{
#endif
int b();
#ifdef __cplusplus
}
#endif
#endif /* Test1_hpp */
Test1.cpp
#include "Test1.hpp"
int b()
{
return 88;
}
二.C++调用C
main.cpp
#include <iostream>
#include <stdio.h>
#include "Test.h"
int main(int argc, const char * argv[]) {
printf("%d",a());
return 0;
}
Test.h
#ifndef Test_h
#define Test_h
#include <stdio.h>
#ifdef __cplusplus
extern "C"{
#endif
int a();
#ifdef __cplusplus
}
#endif
#endif /* Test_h */
Test.c
#include "Test.h"
int a()
{
return 3;
}
总的来说,就是在相关的头文件中添加了如下代码:
#ifdef __cplusplus
extern "C"{
#endif
......(一些方法或者变量的定义,可以让别的调用)
eg:int a();
#ifdef __cplusplus
}
#endif