467,925 Members | 1,660 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

combining c and c++ source trees

* 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
--
vv


Jul 19 '05 #1
  • viewed: 2814
Share:
1 Reply
Vikram wrote:
* 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++.

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Thomas Paul Diffenbach | last post: by
13 posts views Thread by sembiance | last post: by
2 posts views Thread by Confessor | last post: by
3 posts views Thread by ptrSriram | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.