Invoking C++ methods from C functions

Invoking C++ methods from C functions

Post by Sajjad Late » Mon, 23 Nov 1998 04:00:00



Hello all,

        I have to prepare a shared library with a predefined API.
The API is declared as a bunch of C functions.

        In contemplating the design, I find that I can implement
most of the internal data manipulation code as C++ classes. It is a lot
cleaner and easier to understand this way. However, I am not able to
figure out how to instantiate these C++ objects and make calls to
these objects' methods from the C API functions.

        I did briefly glance at dejanews and the C FAQ but did not find
anything applicable. I am (almost) considering implementing the whole thing
in C if it is not possible. However, C++ would be much more preferable.

        Suggestions, comments are welcome.

Thanks in advance.
Sajjad

 
 
 

Invoking C++ methods from C functions

Post by Charles LaCou » Tue, 24 Nov 1998 04:00:00


Although this is clearly not an ANSI C question, can't you do something like
this:

/* C code */
void CppFunc( void );

int main( void )
{
    CppFunc();

    return 0;

Quote:}

// C++ code
class MyClass
{
    // Whatever you want

Quote:};

extern "C" void CppFunc()
{
    MyClass mc;

    // Whatever

Quote:}

--
Increase the Peace!
Charles LaCour


>Hello all,

> I have to prepare a shared library with a predefined API.
>The API is declared as a bunch of C functions.

> In contemplating the design, I find that I can implement
>most of the internal data manipulation code as C++ classes. It is a lot
>cleaner and easier to understand this way. However, I am not able to
>figure out how to instantiate these C++ objects and make calls to
>these objects' methods from the C API functions.

> I did briefly glance at dejanews and the C FAQ but did not find
>anything applicable. I am (almost) considering implementing the whole thing
>in C if it is not possible. However, C++ would be much more preferable.

> Suggestions, comments are welcome.

>Thanks in advance.
>Sajjad



 
 
 

Invoking C++ methods from C functions

Post by Gary McWillia » Tue, 24 Nov 1998 04:00:00


[posted and cited.]


> Hello all,

>    I have to prepare a shared library with a predefined API.
> The API is declared as a bunch of C functions.

>    In contemplating the design, I find that I can implement
> most of the internal data manipulation code as C++ classes. It is a lot
> cleaner and easier to understand this way. However, I am not able to
> figure out how to instantiate these C++ objects and make calls to
> these objects' methods from the C API functions.

>    I did briefly glance at dejanews and the C FAQ but did not find
> anything applicable. I am (almost) considering implementing the whole thing
> in C if it is not possible. However, C++ would be much more preferable.

>    Suggestions, comments are welcome.

> Thanks in advance.
> Sajjad


How about having your 'C' api return a (void*) for each object you
create. Then pass this in to each function you wish to call and cast it
back to the class type?.

'C' API (compiled with C++ compiler!)

extern "C" {
void *newAnObject();
void callCPPFunc(void*);

Quote:}

class clsA{
public:
  someFunc(){/*...*/};

Quote:};

void *newAnObject()
{
        clsA *obj = new clsA ;
        return obj ;

Quote:}

void callCPPFunc( void *this_ )
{
        ((clsA*)this_)->someFunc() ;

Quote:}

'main' app

int main()
{
  void *obj ;
  obj = newAnObject();
  callCPPFunc(obj);
  return 0 ;

Quote:}

--
Gary McWilliams
CCC Technology Ltd, N.Ireland
www.cccgroup.co.uk
 
 
 

Invoking C++ methods from C functions

Post by Bill » Tue, 24 Nov 1998 04:00:00


I have the problem, but need to run on the SGI IRIX 6.5. I wrote
something similiar(I realize you are only using the code for
illustrative purposes and have omitted the headers for brevity) but I
couldn't get my code to compile on the SGI with the SGI linker and
compiler suite, but it compiled and linked fine on with g++/gcc. Anyone
know how to link C++ obj files with main.c code on the SGI.

> [posted and cited.]


> > Hello all,

> >       I have to prepare a shared library with a predefined API.
> > The API is declared as a bunch of C functions.

> >       In contemplating the design, I find that I can implement
> > most of the internal data manipulation code as C++ classes. It is a lot
> > cleaner and easier to understand this way. However, I am not able to
> > figure out how to instantiate these C++ objects and make calls to
> > these objects' methods from the C API functions.

> >       I did briefly glance at dejanews and the C FAQ but did not find
> > anything applicable. I am (almost) considering implementing the whole thing
> > in C if it is not possible. However, C++ would be much more preferable.

> >       Suggestions, comments are welcome.

> > Thanks in advance.
> > Sajjad

> How about having your 'C' api return a (void*) for each object you
> create. Then pass this in to each function you wish to call and cast it
> back to the class type?.

> 'C' API (compiled with C++ compiler!)

> extern "C" {
> void *newAnObject();
> void callCPPFunc(void*);
> }

> class clsA{
> public:
>   someFunc(){/*...*/};
> };

> void *newAnObject()
> {
>         clsA *obj = new clsA ;
>         return obj ;
> }

> void callCPPFunc( void *this_ )
> {
>         ((clsA*)this_)->someFunc() ;
> }

> 'main' app

> int main()
> {
>   void *obj ;
>   obj = newAnObject();
>   callCPPFunc(obj);
>   return 0 ;
> }
> --
> Gary McWilliams
> CCC Technology Ltd, N.Ireland
> www.cccgroup.co.uk

 
 
 

Invoking C++ methods from C functions

Post by Sajjad Late » Fri, 27 Nov 1998 04:00:00


        Thank you all. I now have enough ideas to build on.

Sajjad

: [posted and cited.]


: > Hello all,
: >
: >  I have to prepare a shared library with a predefined API.
: > The API is declared as a bunch of C functions.