从c调用c ++函数 [英] call c++ function from c

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

问题描述

你好,


我有问题在C项目中调用c ++函数。我有一个来自cpp文件的

头文件。在头文件中,函数是

decleared" int GetHostIPAddresses(ini i);"。我在c文件中包含头文件

并调用函数GetHostIPAddresses(1);.但

链接器没有找到该功能。有人能给我一个暗示是什么

错了。


祝你好运


Falk

Hello,

I have the problem to call a c++ funtion in a C project. I have a
header file from the cpp file. In the header file the function is
decleared "int GetHostIPAddresses(ini i);". I inlcude the header file
in the c file and call the function GetHostIPAddresses(1);. But the
linker doesn''t found the function. Can someone give me a hint what is
wrong.

Best regards

Falk

推荐答案

fa ** ***********@web.de 写道:

我有问题在C项目中调用c ++函数。我有一个来自cpp文件的

头文件。在头文件中,函数是

decleared" int GetHostIPAddresses(ini i);"。我在c文件中包含头文件

并调用函数GetHostIPAddresses(1);.但

链接器没有找到该功能。有人能给我一个暗示是什么是错误的。
I have the problem to call a c++ funtion in a C project. I have a
header file from the cpp file. In the header file the function is
decleared "int GetHostIPAddresses(ini i);". I inlcude the header file
in the c file and call the function GetHostIPAddresses(1);. But the
linker doesn''t found the function. Can someone give me a hint what is
wrong.



在comp.lang.c ++中问这个可能会更好,因为

这更多是在专业领域那里的常客是

。另见

http://www.parashift.com/c++-faq/mixing-c-and-cpp.html

无论如何我都会尝试一个简短的解释(这可能是错误的因为我

不是C ++专家!):在C ++中你可以有几个功能,

同名,只能用数字来区分和

他们的论点类型。为了让链接器能够区分这些函数,这些函数会自动重命名函数auto-

,通常是通过在名称上添加一些文本

表示参数的数量和类型,例如


foo(int)变为foo_i

foo(double)变为foo_d


(这是一个例子,不是真实的,不同的C ++编译器

可能以不同的方式做到这一点)。如果您知道这个受损的名称然后

您可以相应地更改函数的调用,即在C代码中使用

损坏的名称。显而易见的缺点是,这个

只适用于您确定的C ++编译器如何使用
名称重整。


如果您可以更改C ++源代码并且只有一个具有该名称的
函数,那么您可以使用
在C ++

头文件中声明它。

externC void f(int i,char c,float x);


以防止C ++编译器破坏函数名。


问候, Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de

It probably would be better to ask this in a comp.lang.c++ since
this is more in the territory of expertise of the regulars over
there. See also

http://www.parashift.com/c++-faq/mixing-c-and-cpp.html

I try a short explanation anyway (which might be wrong since I
am not a C++ expert!): In C++ you can have several functons with
the same name that are only distinguished by the number and
types of their arguments. To allow the linker to distinuish be-
tween these functions the compiler renames the functions auto-
matically, typically by appending some text to the names that
indicates the number and types of arguments, like

foo( int ) becomes foo_i
foo( double ) becomes foo_d

(this is an example, not the real thing, different C++ compilers
may do it in different ways). If you know this mangled name then
you can change the call of the function accordingly, i.e. use the
mangled name in the C code. The obvious drawback is that this
only works for the C++ compiler you determined for how it does
the name mangling.

If you can change the C++ source and there''s only a single
function with that name then you can declare it in the C++
header file with

extern "C" void f(int i, char c, float x);

to keep the C++ compiler from mangling the function name.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


fa ************* @ web.de 写道:

>

我有问题在C项目中调用c ++函数。我有一个来自cpp文件的

头文件。在头文件中,函数是

decleared" int GetHostIPAddresses(ini i);"。我在c文件中包含了标题

文件,并调用函数GetHostIPAddresses(1);.

但链接器没有找到该函数。有人能给我一个

暗示有什么不对。
>
I have the problem to call a c++ funtion in a C project. I have a
header file from the cpp file. In the header file the function is
decleared "int GetHostIPAddresses(ini i);". I inlcude the header
file in the c file and call the function GetHostIPAddresses(1);.
But the linker doesn''t found the function. Can someone give me a
hint what is wrong.



你不能这样做。 C ++有调用C函数的规定,但C

对C ++一无所知。即使是C ++调用也只限于同一个编译器。


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>


-

通过 http:// www。中的免费Usenet帐户发布。 teranews.com

You can''t do it. C++ has provisions for calling C functions, but C
doesn''t know anything about C++. Even the C++ calling is
restricted to the same compiler.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com




< fa ************* @ web.dewrote in message

news:11 ********************** @ k79g2000hse.googlegr oups.com ...

<fa*************@web.dewrote in message
news:11**********************@k79g2000hse.googlegr oups.com...

您好,


我在C项目中调用c ++函数时遇到问题。我有一个来自cpp文件的

头文件。在头文件中,函数是

decleared" int GetHostIPAddresses(ini i);"。我在c文件中包含头文件

并调用函数GetHostIPAddresses(1);.但

链接器没有找到该功能。有人能给我一个提示是什么是错误的


Hello,

I have the problem to call a c++ funtion in a C project. I have a
header file from the cpp file. In the header file the function is
decleared "int GetHostIPAddresses(ini i);". I inlcude the header file
in the c file and call the function GetHostIPAddresses(1);. But the
linker doesn''t found the function. Can someone give me a hint what is
wrong.



获取C ++目标文件并将其转储为ascii。你应该看到排列的所有

函数,但因为它是C ++,它们有搞笑的字符和

与它们相关的其他东西。

提取名称 - 可能需要两到三次尝试 - 将其放入您的C />
来源,并尝试链接。最终你会成功。然后仔细测试函数

以确保C ++没有传递任何额外的参数。


-

免费游戏和编程好东西。
http://www.personal。 leeds.ac.uk/~bgy1mm

Get the C++ object file and dump it as ascii. You should see all the
functions arrayed, but because it is C++ they have funny characters and
other things associated with them.
Extract the name - which may take two or three attempts - put it in your C
source, and try to link. Eventually you will succeed. Then test the function
carefully to make sure C++ isn''t passing it any extra arguments.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


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

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