How to mix C code in a C++ program

M

Marko.Cain.23

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.
 
V

Victor Bazarov

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
 
Q

qvasic

i have a c++ program and I want to call a c function (some legacy

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

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
M

Marko.Cain.23

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

Jim Langston

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.
 
R

Ralph D. Ungermann

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.
 
R

Roland Pibinger

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
 
I

Ian Collins

Roland said:
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.
 
S

Sherm Pendley

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--
 
D

david.baird

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.
 
R

Roland Pibinger

Well that special case covers just about every C library!

The original question was:

You don't need extern "C" to compile this source code with a C++
compiler.

Best wishes,
Roland Pibinger
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top