By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

How to mix C code in a C++ program

P: n/a
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.

Mar 19 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Ma***********@gmail.com wrote:
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 .
Declare it with 'extern "C" ':

extern "C" int initproducer();

in C++
>
But when I runs it I get this error:

./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv
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
Mar 19 '07 #2

P: n/a
i have a c++ program and I want to call a c function (some legacy
code).
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();
}

Mar 19 '07 #3

P: n/a
On 2007-03-19 21:27, Ma***********@gmail.com wrote:
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
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
Mar 19 '07 #4

P: n/a
On Mar 19, 3:35 pm, qva...@rambler.ru wrote:
i have a c++ program and I want to call a c function (some legacy
code).

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();

}
Thanks. I resolve my earlier linker error by adding this in my .cpp
file:
extern "C" {
#include "buffer.h"
#include "globalerror.h"
#include "sharedsum.h"
}

But now i get a new one:

../Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: seterror

But in this case, only my c files uses that seterror function (defined
in globalerror.h"). So why I still get 'undefined symbol: seterror'.

Thank for your any help.

$ grep -r seterror *.*
globalerror.h:int seterror(int error);
randconsumer.c: seterror(error);
randconsumer.c: return (seterror(error));
randproducer.c: seterror(error);
randproducer.c: return (seterror(error));
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);


Mar 19 '07 #5

P: n/a
<Ma***********@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
On Mar 19, 3:35 pm, qva...@rambler.ru wrote:
i have a c++ program and I want to call a c function (some legacy
code).

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();

}

Thanks. I resolve my earlier linker error by adding this in my .cpp
file:
extern "C" {
#include "buffer.h"
#include "globalerror.h"
#include "sharedsum.h"
}

But now i get a new one:

./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: seterror

But in this case, only my c files uses that seterror function (defined
in globalerror.h"). So why I still get 'undefined symbol: seterror'.

Thank for your any help.

$ grep -r seterror *.*
globalerror.h:int seterror(int error);
randconsumer.c: seterror(error);
randconsumer.c: return (seterror(error));
randproducer.c: seterror(error);
randproducer.c: return (seterror(error));
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
I don't know, that's fairly linux specific (dealing with g++'s linker). If
no one here responds, try a linux newsgroup.
Mar 20 '07 #6

P: n/a
Ma***********@gmail.com wrote:
Thanks. I resolve my earlier linker error by adding this in my .cpp
file:
extern "C" {
#include "buffer.h"
#include "globalerror.h"
#include "sharedsum.h"
}

But now i get a new one:

./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: seterror

But in this case, only my c files uses that seterror function (defined
in globalerror.h"). So why I still get 'undefined symbol: seterror'.

Thank for your any help.

$ grep -r seterror *.*
globalerror.h:int seterror(int error);
randconsumer.c: seterror(error);
randconsumer.c: return (seterror(error));
randproducer.c: seterror(error);
randproducer.c: return (seterror(error));
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
sharedsum.c: return seterror(error);
I'm missing:
globalerror.c: int seterror(int error)

Obviously, you forgot to define the function.
Mar 20 '07 #7

P: n/a
On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
<Erik-wikstrom@...comwrote:
>On 2007-03-19 21:27, Ma***********@gmail.com wrote:
>i have a c++ program and I want to call a c function (some legacy
code).
But when I runs it I get this error:
./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv

In the header-file of the c-code you need to sorround the
function-declarations with this:

#ifdef __cplusplus
extern "c" {
#endif
Only in the special case when a C library is linked to a C++ program
and a C header is used. Since C++ is mostly a superset of C, code
written in C compiles as C++ provided that the incompatibilities
between the two languages are amended.

Best regards,
Roland Pibinger
Mar 20 '07 #8

P: n/a
Roland Pibinger wrote:
On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
<Erik-wikstrom@...comwrote:
>>On 2007-03-19 21:27, Ma***********@gmail.com wrote:
>>>i have a c++ program and I want to call a c function (some legacy
code).
But when I runs it I get this error:
./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv

In the header-file of the c-code you need to sorround the
function-declarations with this:

#ifdef __cplusplus
extern "c" {
#endif


Only in the special case when a C library is linked to a C++ program
and a C header is used.
Well that special case covers just about every C library!
Since C++ is mostly a superset of C, code
written in C compiles as C++ provided that the incompatibilities
between the two languages are amended.
But C libraries generally get compiled as C, with a C compiler.

--
Ian Collins.
Mar 20 '07 #9

P: n/a
rp*****@yahoo.com (Roland Pibinger) writes:
On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
<Erik-wikstrom@...comwrote:
>>On 2007-03-19 21:27, Ma***********@gmail.com wrote:
>>i have a c++ program and I want to call a c function (some legacy
code).
But when I runs it I get this error:
./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
asncfsys.so: undefined symbol: _Z12initproducerv

In the header-file of the c-code you need to sorround the
function-declarations with this:

#ifdef __cplusplus
extern "c" {
#endif

Only in the special case when a C library is linked to a C++ program
and a C header is used.
Given that the error given above is almost certainly the result of not
doing so in just such a case... what's your point?

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
Mar 20 '07 #10

P: n/a
On Mar 19, 2:50 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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.
I agree. This is the answer.

All header files containing C functions should be written this way.

Mar 21 '07 #11

P: n/a
On Wed, 21 Mar 2007 09:35:20 +1200, Ian Collins wrote:
>Roland Pibinger wrote:
>Only in the special case when a C library is linked to a C++ program
and a C header is used.
Well that special case covers just about every C library!
The original question was:
On 19 Mar 2007 13:27:24 -0700, Marko.Cain.23@...com wrote:
>>>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 .
You don't need extern "C" to compile this source code with a C++
compiler.

Best wishes,
Roland Pibinger
Mar 21 '07 #12

P: n/a
On Tue, 20 Mar 2007 18:14:01 -0400, Sherm Pendley <sp******@dot-app.orgwrote:
rp*****@yahoo.com (Roland Pibinger) writes:
>On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
<Erik-wikstrom@...comwrote:
....
>>>In the header-file of the c-code you need to sorround the
function-declarations with this:

#ifdef __cplusplus
extern "c" {
#endif

Only in the special case when a C library is linked to a C++ program
and a C header is used.

Given that the error given above is almost certainly the result of not
doing so in just such a case... what's your point?
I assume his point is that the first, obvious solution to the question
in the subject line ("How to mix C code in a C++ program") is "compile
it with a C++ compiler".

Linking to a C library is usually not referred to as "mixing C code",
and some people regularly borrow code at the source level -- so IMHO
it makes sense to mention this case too.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Mar 26 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.