calling C++ functions from C

calling C++ functions from C

Post by #Rachid Him » Sat, 28 Sep 1991 18:09:27



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++])());

Quote:}

------end of _main()------------------------

Bye

Rachid HIMMI

 
 
 

calling C++ functions from C

Post by Stephen D. Clama » Sun, 29 Sep 1991 01:19:17



>"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.

Quote:>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.

Quote:>"_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.

--



 
 
 

1. Calling a C-function in a C++ program when c-function needs function pointer

I have some c-code I half to rewrite to c++ and in this proces I half to
make a function call from c++ to a c-function

I have declared the c-function in extern "C" and rewritten the C-functions
parameterlist to c++ style!

Here is the c-functions declaration:
int lmdif_(int (*fcn) (),integer *m,integer *n,doublereal *x,doublereal
*fvec,
 doublereal *ftol,doublereal *xtol,doublereal *gtol,integer *maxfev,
doublereal *epsfcn,doublereal *diag,integer *mode,doublereal *factor,integer
*nprint,
integer *info,integer *nfev,doublereal *fjac,integer *ldfjac,integer *ipvt,
doublereal *qtf,doublereal *wa1,doublereal *wa2,doublereal *wa3,doublereal
*wa4)

Here is the extern "C" declaration:

#ifndef CAL_MAIN_H_
#define CAL_MAIN_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
 lmdif_(int (*fcn)(),integer *m,integer *n,doublereal *x,doublereal *fvec,
 doublereal *ftol,doublereal *xtol,doublereal *gtol,integer *maxfev,
doublereal *epsfcn,doublereal *diag,integer *mode,doublereal *factor,integer
*nprint,
integer *info,integer *nfev,doublereal *fjac,integer *ldfjac,integer *ipvt,
doublereal *qtf,doublereal *wa1,doublereal *wa2,doublereal *wa3,doublereal
*wa4);
 void  distorted_to_undistorted_sensor_coord (double Xd,double Yd,double
*Xu,double *Yu);
 void  distorted_to_undistorted_image_coord (double Xd,double Yd,double
*Xu,double *Yu);
#ifdef __cplusplus
#endif /* __cplusplus */

#endif /* !CALMAIN_H_ */

Here i a call of the c-function lmdif_():
    lmdif_ (cc_compute_exact_f_and_Tz_error,
            &m, &n, x, fvec, &ftol, &xtol, &gtol, &maxfev, &epsfcn,
            diag, &mode, &factor, &nprint, &info, &nfev, fjac, &ldfjac,
            ipvt, qtf, wa1, wa2, wa3, wa4);

I get the error that the first parameter in the call
cc_compute_exact_f_and_Tz_error of type void(long*, long*, double *, double
*) cannot be converted to int(__cdecl *)(void)

What can I do to remove the error?
Could I cast the parameret in the call to the type  "int(__cdecl *)(void)"
and how would I do this?

--
Benny Lund
Student at Computer Science
at Aalborg University


home phone: +4599348438

2. Web Explorer v0.92 breaks FTP for us!! ** IBM please note **

3. Calling C++ functions from C functions

4. BSOD & Spontaneous reboot

5. Try my new game!

6. Call to private member function calls another function in another class!

7. HRESULT Lookup Tool...

8. Calling object's function,or calling a function on an object?

9. C++ : Calling MS DLL functions from Borland C++ crashes my program

10. C++ : Calling MS DLL exported functions from Borland C++ Crashes my App.

11. Q: Borland C++ (5.0) calling function in MS Fortran DLL

12. C++ member functions as call-backs