900626-如何从C内调用C ++函数 [英] 900626 - how to call a C++ function from within C
本文介绍了900626-如何从C内调用C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void fc(){...}
并且在C ++模块中定义了此功能:
无效 fcpp(){...}
----
当我们想从fcpp中调用fc时,就足以给出这样的原型:
外部 " fc();
那么我们可以称之为.
但是,当我们想从fc内部调用fcpp怎么办?给出这样的原型是胡说八道:
外部 " 无效 fcpp();
----
在这种情况下我该怎么办?
thx
解决方案
在第一种情况下,您应该使用extern "C"
.这将确保使用"C"类型链接创建C ++函数,因此可以从C代码进行访问.对于引用fcpp()
的所有外部C ++模块,您也将需要此定义,以便使用"C"格式调用约定对其进行编译.
hi
consider two modules in a project, one be C and one be C++. assume that in the C module this function is defined:
void fc() {...}
and in the C++ module this function is defined:
void fcpp() {...}
----
when we want to call fc from within fcpp it''s enough to give such a prototype:
extern "C" fc();
then we may call it.
but what about when we want to call fcpp from within fc? it''s nonsense to give such a prototype:
extern "C++" void fcpp();
----
what can i do in such a case?
thx
解决方案
You should useextern "C"
as in the first case. This will ensure that the C++ function is created with ''C'' type linkage, and so may be accessed from C code. You will also need this definition for any external C++ modules that refer tofcpp()
so that they are compiled with the ''C'' format calling conventions.
这篇关于900626-如何从C内调用C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文