C函数在C ++代码中 [英] C function in a C++ code
问题描述
我有一个基于C的代码,但主代码是用C ++编写的,所以我想从C ++文件中调用
函数。 br />
我添加了
externC
{
} <在C ++代码的开头是
。
我所做的就是发送到数组并返回一个数组。它确实没有合适,所以我在发送给C
函数之前写了第一个数字,当C代码得到它时。
>
当我发送时它是1.186674但是当函数得到它时它是0.611370。
这是怎么回事?在C ++中使用C函数还有什么其他事吗?
干杯...
Hi,
I have a C based code but the main code is in C++ so I want to call the
function from C++ file.
I have added
extern "C"
{
}
at the begining of the C++ code.
All I am doing is sending to arrays and getting back a single one. It does
not come right, so I have writtine out the first number before sending to C
function and when the C code gets it.
it is 1.186674 when I am sending but it is 0.611370 when function get it.
How does this happend? Anything else to do to use a C function in C++?
Cheers...
推荐答案
" kak3012" < S0 ***** @ student.dtu.dk>在消息中写道
news:ct *********** @ gnd.k-net.dk ...
"kak3012" <s0*****@student.dtu.dk> wrote in message
news:ct***********@gnd.k-net.dk...
我有一个基于C的代码,但主要代码是用C ++编写的,所以我想从C ++文件中调用
函数。
我添加了
> externC
{
}
在C ++代码的开头。
如果这是代码是逐字的,它绝对没有效果。
我所做的就是发送到数组并返回一个数组。它确实没有成功,所以我在发送到
C函数之前写了第一个数字,当C代码得到它时。
当我发送时它是1.186674但是当功能得到它时它是0.611370。
这是怎么回事?在C ++中使用C函数还有什么其他事吗?
Hi,
I have a C based code but the main code is in C++ so I want to call the
function from C++ file.
I have added
extern "C"
{
}
at the begining of the C++ code.
If that''s the code verbatim, It will have absolutely no effect.
All I am doing is sending to arrays and getting back a single one. It does
not come right, so I have writtine out the first number before sending to C function and when the C code gets it.
it is 1.186674 when I am sending but it is 0.611370 when function get it.
How does this happend? Anything else to do to use a C function in C++?
/ * file.c * /
#include< stdio.h>
void c_function(void)
{
puts(" Hello from c_function()");
}
/ * file.cpp * /
extern" C"
{
void c_function(void);
}
int main()
{
c_function();
返回0;
}
1.使用C编译器编译''file.c''
2.编译''file.cpp''用C ++编译器。
3.链接1.和2的输出来创建
一个可执行程序
许多编译器可以充当C或C ++编译器,
因此单个产品通常能够处理
和1和2。查看文档以获取详细信息。
链接超出了C ++语言的范围,
所以请再次检查文档以了解如何执行此操作。 />
-Mike
/* file.c */
#include <stdio.h>
void c_function(void)
{
puts("Hello from c_function()");
}
/* file.cpp */
extern "C"
{
void c_function(void);
}
int main()
{
c_function();
return 0;
}
1. Compile ''file.c'' with a C compiler.
2. Compile ''file.cpp'' with a C++ compiler.
3. Link the outputs from 1. and 2. to create
an executable program
Many compilers can act as either a C or C++ compiler,
so a single product will often be able to handle
both 1. and 2. Check the documentation for details.
Linking is beyond the scope of the C++ language,
so again, check your documentation for how to do that.
-Mike
kak3012写道:
kak3012 wrote:
我有一个C基于代码,但主要代码是C ++,所以我想从C ++文件调用
函数。
我已经添加了
externC
我正在做的就是发送到数组并返回一个数组。它
不对,所以我在发送到C函数之前写了第一个数字,当C代码得到它时。
当我发送时它是1.186674但是当功能得到它时它是0.611370。
这是怎么回事?在C ++中使用C函数还有什么其他事吗?
Hi,
I have a C based code but the main code is in C++ so I want to call the
function from C++ file.
I have added
extern "C"
{
}
at the begining of the C++ code.
All I am doing is sending to arrays and getting back a single one. It
does not come right, so I have writtine out the first number before
sending to C function and when the C code gets it.
it is 1.186674 when I am sending but it is 0.611370 when function get it.
How does this happend? Anything else to do to use a C function in C++?
对话很便宜,显示一些代码! :-)
-
WW aka Attila
:::
根据我的说法计算问题不存在。
Talk is cheap, show some code! :-)
--
WW aka Attila
:::
According to my calculations the problem doesn''t exist.
您好,
您是否以正确的数据类型发送值?可能是你在使用cout发送C ++部分时打印了这个值。接收C部分的
使用printf打印。尝试在两个地方使用相同的
功能。并使用相同的数据类型。
实际上,C函数也是一个C ++函数。您可以直接在C ++源代码中编译C
函数。无论如何,如果你发布一部分处理错误的代码,那将会很好。
Hello,
Are you sending the value in the right datatype? It might be that
you printed the value while sending in C++ part using "cout" and
receiving in C part is printed using "printf". Try to use same
functions in both the places. And use the same datatype as well.
Actually, a C function is a C++ function as well. you can compile a C
function directly in a C++ source code. Anyway, it will be good if you
post a part of the code that deals with the error.
这篇关于C函数在C ++代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!