Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > combining c and c++ source trees

Reply
Thread Tools

combining c and c++ source trees

 
 
Vikram
Guest
Posts: n/a
 
      06-27-2003
* 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




 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-27-2003
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++.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary search trees (AVL trees) jacob navia C Programming 34 01-08-2010 07:27 PM
Problem comparing object graphs and trees raphael.marvie@gmail.com Python 3 12-15-2006 02:10 PM
Xerces C++ and DOM trees Peter Saffrey XML 1 01-06-2005 03:21 PM
JAXB : comparing and updating trees Jean-Marie Condom Java 0 12-28-2004 05:20 PM
STL and Balanced Trees??? Peter Tkacz C++ 7 10-24-2003 06:08 AM



Advertisments