Topic: calling C++ functions from C
Author: rh@jupiter.UUCP (#Rachid Himmi)
Date: 18 Sep 91 07:02:37 GMT Raw View
Josh Lam writes
>>I need help on how to call C++ functions from C.
>>
>>I am currently working on a Sun Unix environment. I have some C++
>>functions already available. How can I write a C program to call these C++
>>functions (I will be compiling the C program with a C compiler).
>>
>>Would really appreciate some help!
calling C++ functions from C program is relatively easy but some precautions
have to be taken.
I will considere here an example under Sun environment.
the aim is to call C++ member functions foo() of a class A
from a main C program...
Assume the C++ file (named cxxfile.cc) contains following instructions:
class A
{
...
int foo(int x);
...
};
...
A data; /* declaration of an object A */
...
extern "C" int cxxfoo(int x)
{
data.foo(x);
}
Then the C program (main.c) has to contain:
...
extern int cxxfoo(int);
...
main()
{
_main();
...
cxxfoo(36);
...
}
Now follow next steps:
1/ Generate a C source file from your C++ file
CC -Fc cxxfile.cc >cxxfile.c
2/ compile all C source file in order to produce object files
cc -c cxxfile.c -o cxxfile.o
cc -c main.c -o main.o
3/ build a C source file that allow call of static constructors/destructors
nm cxxfile.o | munch > cxxconstruct.c
-nm gives symbol table of each object file
-munch build a C source file containing initialization of ctors and
dtors variables (called by _main() )
4/ compile the generated file
cc -c cxxconstruct.c -o cxxconstruct.o
5/ link all objects files with C++ library (e.g. in Sun4 environment)
cc -o main main.o cxxfile.o cxxconstruct.o -L /usr/CC/sun4 -lC
It works correctly and I think it will resolve your problem.
Good Luck !
Rachid HIMMI (emai rh@absstbg.uucp)
Author: josh@saifr00.cfsat.honeywell.com (Josh Lam)
Date: 18 Sep 91 15:48:09 GMT Raw View
Hi Rachid
Thanks for your reply!
In your solution, you advised that I should use the following:
nm cxxfile.o | munch > cxxconstruct.c
What is 'munch'? When I ran the above, I received a message:
munch: Command not found.
My first guess is that munch is a script that I do not have. If it is,
would it be possible to send me a copy of it?
Also, can you please comment a little about _main()?
Thanks again!
Josh Lam
Author: rh@jupiter.UUCP (#Rachid Himmi)
Date: 27 Sep 91 09:09:27 GMT Raw View
hi Josh (sorry for my lateness!!)
"munch" is an executable file provided (I suppose in standard) by SUN with
the C++ environment.
On my machines it is located in /usr/CC/sun4 (or /usr/CC/sun3).
If this executable is not available on your machine, you can easily build it
if you know the rules of C++ mangling symbols.
For example the syntaxe for static C++ constructors is
__sti___foo_()
and the corresponding C file generated by munch is
----------C source file generated by munch----------
typedef int (*PFV)();
int __sti___foo_();
extern PFV _ctors[];
PFV _ctors[] = {
__sti___foo_;
0
};
--------end of file--------------------------------
"_main.c" is a very simple function that has in charge of calling all static
constructors stored in "ctors" array (see previous initialiZation by munch)
An example of this function is given here
--------_main() definition------------------
typedef int (*pfunc)();
extern pfunc _ctors[];
_main()
{
int i ;
for (i = 0; _ctors[i] ; (*_ctors[i++])());
}
------end of _main()------------------------
Bye
Rachid HIMMI
Author: steve@taumet.com (Stephen D. Clamage)
Date: 27 Sep 91 16:19:17 GMT Raw View
rh@jupiter.UUCP (#Rachid Himmi) writes:
>"munch" is an executable file provided (I suppose in standard) by SUN with
>the C++ environment.
>On my machines it is located in /usr/CC/sun4 (or /usr/CC/sun3).
"munch" is specific to C++ compilers derived from AT&T Cfront. Some
compilers derived from Cfront use "patch" instead of munch. I know
of no other C++ compilers which use either, as they are not required
when generating object code directly.
>If this executable is not available on your machine, you can easily build it
>if you know the rules of C++ mangling symbols.
These rules are different for every compiler. Even different releases
of Cfront have used different name-mangling schemes. Name mangling is
not part of the C++ language specification, but is merely a convenient
implementation technique.
>"_main.c" is a very simple function that has in charge of calling all static
>constructors stored in "ctors" array (see previous initialiZation by munch)
"_main.c" is used only by C++ compilers derived from Cfront. Other
compilers use other techniques to call static constructors.
--
Steve Clamage, TauMetric Corp, steve@taumet.com