小标
2018-08-10
来源 :
阅读 1432
评论 0
摘要:本文主要向大家介绍了C/C++知识点之C 程序与 C++ 程序间的相互调用,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之C 程序与 C++ 程序间的相互调用,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
因为 C 编译器编译函数时不带参数的类型信息,只包含函数的符号名字。如 void foo( int x ) , C 编译器会将此函数编译成类似 _foo 的符号,C 链接器只要找到了调用函数的符号,就会认为链接成功。而 C++ 编译器为了实现函数重载,会在编译时带上函数的参数信息。如它可以把上面的函数编译成类似于 _foo_int 这样的符号。
所以在 C++ 与 C 相互调用时,要用 extern "C" 加以声明。
下面用两个示例实现它们之间的相互调用(没有优化代码,只是起示例作用)。
C++ 调用 C 函数
/* cfile.c */
#include <stdio.h>
void C_Func()
{
puts("Hello, I'm C_Func");
}
/* cppfile.cpp */
#include <iostream>
extern "C" void C_Func(void);
int main()
{
C_Func();
system("pause");
return 0;
}
C 调用 C++ 内类的成员函数
/* cppfile.cpp */
#include <iostream>
#include "cpphfile.h"
using namespace std;
void Myclass::print()
{
cout << "Hello, I'm print function." << endl;
}
/* cpphfile.h */
#ifndef _CPPCLASS_H__
#define _CPPCLASS_H__
class Myclass{
public:
void print();
};
#endif
/* wrapper.cpp 这个文件实现调用 C++ 的接口*/
#include "cpphfile.h"
extern "C" {
struct wrapper_class{
Myclass w_class;
};
struct wrapper_class* get_object()
{
return new struct wrapper_class;
}
void call_cpp_print(struct wrapper_class* p)
{
p->w_class.print();
}
}
/* cfile.c */
#include <stdio.h>
struct wrapper_class *test;
int main()
{
/* 创建对象 */
test = get_object();
/* 调用 C++ 函数 */
call_cpp_print(test);
system("pause");
}
小总结
extern "C" 应该只能在 CPP 文件或被引入到 CPP 中的头文件内声明。
通常应该这样使用 extern "C":
#ifdef __cplusplus
extern "C" {
#endif
/* your text */
#ifdef __cplusplus
}
#endif
C++ 编译器中已经定义了 __cplusplus 这个宏。
本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号