c interface with c++ implementation

Discussion in 'C++' started by zade, Jul 9, 2007.

  1. zade

    zade Guest

    I want to create a library with c interface but with c++
    implementation. But I don't know if there exist some potential
    problems.
    Any advice? thanks!
     
    zade, Jul 9, 2007
    #1
    1. Advertising

  2. On 7 9 , 5 24 , zade <> wrote:
    > I want to create a library with c interface but with c++
    > implementation. But I don't know if there exist some potential
    > problems.
    > Any advice? thanks!


    There's no potential problem, As far as I did in my previous project

    Here's a small example

    //A.h
    class A {
    public:
    A(char);
    int Func(int);
    };

    // c_interface.h
    #ifdef __cplusplus
    extern "C" {
    #endif // __cplusplus

    typedef void* A_HANDLE;

    A_HANDLE create_A(char c);

    void destroy_A(A_HANDLE h);

    int c_func(A_HANDLE, int);
    // other C functions ...
    #ifdef __cplusplus
    }
    #endif // __cplusplus

    // a.cpp
    #include "a.h"
    #include "c_interface.h"

    A::A(char c) {
    }

    int A::Func(int i) {

    }

    A_HANDLE create_A(char c) {
    new A(c);
    }

    void destroy_A(A_HANDLE h) {
    delete static_cast<A*>(h);
    }

    int c_func(A_HANDLE h, int i) {
    A* pA = static_cast<A*>(h);
    return pA->Func(i);
    }

    //test.c
    #include "c_interface.h"
    A_HANDLE hA = create_A('a');

    int ret = c_func(10);

    destroy_A(hA);
     
    comp.lang.c++.moderated, Jul 9, 2007
    #2
    1. Advertising

  3. On Mon, 09 Jul 2007 03:48:14 -0700, <dhb2000@...com> wrote:
    >On 7 9 , 5 24 , zade <> wrote:
    >> I want to create a library with c interface but with c++
    >> implementation. But I don't know if there exist some potential
    >> problems.

    >
    >There's no potential problem, As far as I did in my previous project


    There may be problems, e.g. with the initialization of global
    variables.

    >Here's a small example
    >
    >//A.h
    >class A {
    >public:
    > A(char);
    > int Func(int);
    >};
    >
    >// c_interface.h
    >#ifdef __cplusplus
    >extern "C" {
    >#endif // __cplusplus
    >
    >typedef void* A_HANDLE;


    No need to use a void* here.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
     
    Roland Pibinger, Jul 9, 2007
    #3
  4. On 7 9 , 7 57 , (Roland Pibinger) wrote:
    > On Mon, 09 Jul 2007 03:48:14 -0700, <dhb2000@...com> wrote:
    > >On 7 9 , 5 24 , zade <> wrote:
    > >> I want to create a library with c interface but with c++
    > >> implementation. But I don't know if there exist some potential
    > >> problems.

    >
    > >There's no potential problem, As far as I did in my previous project

    >
    > There may be problems, e.g. with the initialization of global
    > variables.


    just define the global variables in corresponding the cxx files, and
    define pointer to the variables which can be accessed in c files, also
    provide getter/setter functions if needed.
    Anyway, the implementation is tedious.

    >
    > >Here's a small example

    >
    > >//A.h
    > >class A {
    > >public:
    > > A(char);
    > > int Func(int);
    > >};

    >
    > >// c_interface.h
    > >#ifdef __cplusplus
    > >extern "C" {
    > >#endif // __cplusplus

    >
    > >typedef void* A_HANDLE;

    >
    > No need to use a void* here.


    type define produce memorial type names
    >
    > --
    > Roland Pibinger
    > "The best software is simple, elegant, and full of drama" - Grady Booch
     
    comp.lang.c++.moderated, Jul 9, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maciek Zywno
    Replies:
    0
    Views:
    373
    Maciek Zywno
    Feb 19, 2004
  2. Will
    Replies:
    3
    Views:
    5,850
  3. Michael Tsang
    Replies:
    32
    Views:
    1,123
    Richard Bos
    Mar 1, 2010
  4. Michael Tsang
    Replies:
    54
    Views:
    1,204
    Phil Carmody
    Mar 30, 2010
  5. sanket
    Replies:
    7
    Views:
    1,019
    Tsung
    Nov 3, 2011
Loading...

Share This Page