combining c and c++ source trees

V

Vikram

* i have a file foo.cpp ; it has a single function called
"call_me(char*)". the function does some stuff and uses some other large
libraries written in C++.
* i also have bar.c ; i need to call this single function [call_me] from
the bar.c code. bar.c is a part of a large C code base.

how do i do this ? as far as i understand i did the following:

1) extern "C" {void call_me(char*);} for the header foo.h and then
included foo.h in bar.c.
2) then i compiled foo.cpp into foo.o using g++ - this works
3) then i compiled bar.c and foo.o using gcc but i get a ton of errors at
this stage. all relating to c++ stl stuff. what is wrong here ?

and in general what is the correct/easy way of mixing C and C++ code -
ideally i would like to call functions from both sides into the other.

hope i was clear. thanks
 
R

Rolf Magnus

Vikram said:
* i have a file foo.cpp ; it has a single function called
"call_me(char*)". the function does some stuff and uses some other
large libraries written in C++.
* i also have bar.c ; i need to call this single function [call_me]
from the bar.c code. bar.c is a part of a large C code base.

how do i do this ? as far as i understand i did the following:

1) extern "C" {void call_me(char*);} for the header foo.h and then
included foo.h in bar.c.

Yes, though you can leave out the curly braces:

extern "C" void call_me(char*);

They are only needed if you want more than one extern declaration and
don't want to repeat the extern keyword all the time.
2) then i compiled foo.cpp into foo.o using g++ - this works
3) then i compiled bar.c and foo.o using gcc but i get a ton of errors
at this stage. all relating to c++ stl stuff. what is wrong here ?

How would we know? "i compiled... but i get a ton of errors" doesn't
really qualify as a useful error description. Anyway, just a wild
guess: you said you "compiled" bar.c and foo.o using gcc. That looks to
me as if you compiled bar.c _and_ in the same stage linked the
resulting object together with foo.o into an executable using gcc. But
if you have c++ sources, you must link with g++. If not, the C++
standard library will not be linked in. So do something like:

g++ -c foo.cpp -o foo.o
gcc -c bar.c -o bar.o
g++ bar.o foo.o -o foobar
and in general what is the correct/easy way of mixing C and C++ code -
ideally i would like to call functions from both sides into the other.

You just have to make a C interface. In the header, write something
like:

#ifdef __cplusplus
extern "C" {
#endif

//your declarations

#ifdef __cplusplus
}
#endif

Then you can #include it from C sources as well as from C++ sources.
And when using gcc/g++, you _must_ link with g++.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top