Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   combining c and c++ source trees (http://www.velocityreviews.com/forums/t267791-combining-c-and-c-source-trees.html)

Vikram 06-27-2003 07:31 AM

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





Rolf Magnus 06-27-2003 09:03 AM

Re: combining c and c++ source trees
 
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++.



All times are GMT. The time now is 10:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.