C和C++相互调用

来源:互联网 发布:我的世界pe联机枪械js 编辑:程序博客网 时间:2024/03/29 20:21

一.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 __cplusplusextern "C"{#endifint 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 __cplusplusextern "C"{#endifint a();#ifdef __cplusplus}#endif#endif /* Test_h */

Test.c

#include "Test.h" int a(){    return 3;}




总的来说,就是在相关的头文件中添加了如下代码:

#ifdef __cplusplusextern "C"{#endif......(一些方法或者变量的定义,可以让别的调用)eg:int a();#ifdef __cplusplus}#endif
0 0
原创粉丝点击