如何从c代码调用C ++函数? [英] How to call a C++ function from c code?

查看:95
本文介绍了如何从c代码调用C ++函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从c代码调用C ++函数?

externC ++?


我认为它只是禁止警告。

我可能要考虑c ++对象创建问题。


C ++是否需要是一个静态函数?


BTW,c代码用C ++编译器编译。

How to call a C++ function from c code?
extern "C++"?

I think it just inhibit the warning.
I maybe have to consider c++ object creation problem.

Does the C++ need to be a static function?

BTW, the c code is compiled with C++ compiler.

推荐答案

" AL @ TW"写道:
"AL@TW" wrote:

>

如何从c代码调用C ++函数?

extern" C ++" ;?
>
How to call a C++ function from c code?
extern "C++"?



No.一般来说,你不能,因为C ++重载。你可以用$ C $ b从C ++调用C.然后。


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。

No. In general, you can''t, because of C++ overloading. You can
call C from C++, however.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


AL @ TW写道:
AL@TW wrote:

如何从c代码调用C ++函数?

extern C ++?


我认为它只是禁止警告。

我可能要考虑c ++对象创建问题。
How to call a C++ function from c code?
extern "C++"?

I think it just inhibit the warning.
I maybe have to consider c++ object creation problem.



这是对comp.lang.c的OT; comp.lang.c ++在大厅的左边。


< OT>

我认为你会得到的答案是C ++函数声明

应该包含在externC中。 {...}阻塞在C ++中编译时,
而C编译器看到的原型声明应该_not_

如此封闭。通常的方法是在单个

标头中使用BEGIN_C_DECLS和

END_C_DECLS宏(Google用于惯用定义),这两种语言的编译器都可以使用它。


我不记得C ++编译器是否需要使用externC封装的函数定义

{...}或是否会记住只需通过附上声明即可编译函数C风格。

This is OT for comp.lang.c; comp.lang.c++ is down the hall, to the left.

<OT>
The answer I assume you''ll get is that the C++ function declaration
should be enclosed in an extern "C" { ... } block when compiled in C++,
while the prototype declaration that the C compiler sees should _not_ be
so enclosed. The usual way to do this is with the BEGIN_C_DECLS and
END_C_DECLS macros (Google for the idiomatic definitions) in a single
header that can be used by compilers of both languages.

I don''t recall if the C++ compiler needs the function definition to be
enclosed with extern "C" { ... } or whether it will "remember" to
compile the function C-style simply by having the declaration enclosed.


C ++是否需要是静态函数?
Does the C++ need to be a static function?



否但,C ++的某些功能在一个函数中是不可用的,因为
被编译为externC。 ,就像默认参数一样,变量

签名等。我很确定没有办法用类

方法来做,无论是静态的还是不,只是简单的功能。常见的

策略是具有C兼容的包装器。函数,它反过来调用真正的C ++函数。 (例如,一个对象可以通过void *参数传递给

包装器,然后包装器将它转换为

对象并调用一个方法结果。)

< / OT>

No. However, some features of C++ aren''t available in a function that
is being compiled as extern "C", like default arguments, variable
signatures, etc. I''m pretty sure there''s no way to do it with a class
method either, whether static or not, just plain functions. A common
tactic is to have a C-compatible "wrapper" function, which in turn calls
the real C++ functions. (For instance, an object may be passed in to
the wrapper via a void* parameter, and the wrapper then casts it to an
object and invokes a method on the result.)
</OT>


BTW,c代码是用C ++编译器编译的。
BTW, the c code is compiled with C++ compiler.



如果你用C ++编译器编译它,代码就是C ++。你可能

已经使用了与C相同的C ++子集(并且希望

具有相同的含义,但这并不保证),但它不是'除非你使用C编译器获得
,否则将获得。


S

If you''re compiling it with a C++ compiler, the code is C++. You may
have used a subset of C++ that looks the same as C (and hopefully has
the same meaning, which isn''t guaranteed), but it isn''t C unless you''re
using a C compiler.

S


" ; AL @ TW" < schosnab ... @ gmail.comwrote:
"AL@TW" <schosnab...@gmail.comwrote:

如何从c代码调用C ++函数?
How to call a C++ function from c code?



你不是。 [你可以在某些情况下,但细节

是特定于实现的,并且超出了C语言本身的范围,因此在comp.lang.c中是偏离主题的。 ]

You don''t. [You can in certain cases, but the details
are implementation specific and outside the scope of
the C language itself, hence off-topic in comp.lang.c.]


extern" C ++"?

我认为它只是禁止警告。

我可能不得不考虑c ++ *对象创建问题。


C ++是否需要是一个静态函数?
extern "C++"?
I think it just inhibit the warning.
I maybe have to consider c++ *object creation problem.

Does the C++ need to be a static function?



在comp.lang.c ++中询问C ++。

Ask about C++ in comp.lang.c++.


BTW,c代码是用C ++编译器。
BTW, the c code is compiled with C++ compiler.



然后你没有C语言问题,而是C ++

问题。再次,请在comp.lang.c ++中询问。


-

Peter

Then you don''t have a C language question, but a C++
question. Again, ask in comp.lang.c++.

--
Peter


这篇关于如何从c代码调用C ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆