如何在C ++程序中混合使用C代码 [英] How to mix C code in a C++ program
问题描述
i有一个c ++程序,我想调用交流功能(一些传统的
代码)。
所以我在我的Producer.c中有这个。
int initproducer(){
printf(" init producer \\\
");
}
我想在我的File.cpp中调用''initproducer()''。
但是当我运行它时,我得到它这个错误:
../Bin/hlxserver:符号查找错误:/ home / cain / install / Plugins /
asncfsys.so:未定义符号: _Z12initproducerv
感谢您的帮助。
Hi,
i have a c++ program and I want to call a c function (some legacy
code).
So I have this in my Producer.c
int initproducer() {
printf (" init producer \n");
}
And I want to call ''initproducer()'' in my File.cpp .
But when I runs it I get this error:
../Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv
Thank you for any help.
推荐答案
Ma *********** @ gmail.com 写道:
i有一个c ++程序,我想调用交流功能(一些传统的
代码)。
所以我在我的Producer.c中有这个。
int initproducer(){
printf(" init producer \\\
" ;);
}
我想在我的File.cpp中调用''initproducer()''。
Hi,
i have a c++ program and I want to call a c function (some legacy
code).
So I have this in my Producer.c
int initproducer() {
printf (" init producer \n");
}
And I want to call ''initproducer()'' in my File.cpp .
用''extern'C"声明它'':
externC int initproducer();
$ c $ b in C ++
Declare it with ''extern "C" '':
extern "C" int initproducer();
in C++
>
但是当我运行它时我收到此错误:
./Bin/hlxserver:符号查找错误:/ home / cain / install / Plugins /
asncfsys.so:undefined symbol :_Z12initproducerv
>
But when I runs it I get this error:
./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv
这是因为C ++编译器不知道函数来自C模块的
。告诉它(见上文)。
V
-
请在回复时删除资金'A'电子邮件
我没有回复最热门的回复,请不要问
That''s because the C++ compiler doesn''t know the function comes
from a C module. Tell it (see above).
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
我有一个c ++程序,我想要调用交流功能(一些遗留的
i have a c++ program and I want to call a c function (some legacy
代码)。
code).
函数,在C中定义,必须在C ++中声明为externC:
// some。 c
void f()
{
printf(" hello,world!\ n";
}
//main.cpp
extern" C" void f();
int main()
{
f();
}
functions, defined in C, must be declared in C++ as extern "C":
//some.c
void f()
{
printf("hello, world!\n";
}
//main.cpp
extern "C" void f();
int main()
{
f();
}
在2007-03-19 21:27, Ma *********** @ gmail .com 写道:
On 2007-03-19 21:27, Ma***********@gmail.com wrote:
i有一个c ++程序,我想调用ac函数(一些遗产
代码)。
所以我在我的Producer.c中有这个。
int initproducer(){
printf(" init producer \ n");
}
我想在我的文件中调用''initproducer()''。 cpp。
但是当我运行它时会出现这个错误:
./Bin/hlxserver:符号查找错误: / home / cain / install / Plugins /
asncfsys.so:未定义的符号:_Z12initproducerv
Hi,
i have a c++ program and I want to call a c function (some legacy
code).
So I have this in my Producer.c
int initproducer() {
printf (" init producer \n");
}
And I want to call ''initproducer()'' in my File.cpp .
But when I runs it I get this error:
./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv
在c-的头文件中您需要使用以下代码来覆盖
函数声明:
#ifdef __cplusplus
extern" c" {
#endif
/ *函数声明* /
#ifdef __cplusplus
}
#endif
这是因为C ++破坏了函数名称以允许重载和
C所做的事情不。这也告诉编译器/链接器在调用这些函数时使用C
调用约定。
-
Erik Wikstr ?m
In the header-file of the c-code you need to sorround the
function-declarations with this:
#ifdef __cplusplus
extern "c" {
#endif
/* function declarations */
#ifdef __cplusplus
}
#endif
This is because C++ mangles the function-names to allow overloading and
stuff which C does not. This also tells the compiler/linker to use the C
calling convention when calling these functions.
--
Erik Wikstr?m
这篇关于如何在C ++程序中混合使用C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!