C++ library access from C

Discussion in 'C++' started by TBass, Sep 29, 2006.

  1. TBass

    TBass Guest

    Hi,

    This is going to be one of those annoyingly stupid questions.

    I would like to compile a library as a C++ program, but it needs to be
    able to work with a C program as well. I only want to distribute one
    ..lib file and an H file.

    I know that I can call a C library from a C++ program. Is it possible
    to compile a CPP library that can be called from a C program?

    For the, "why not just make it a C library and stop wasting our time?"
    question, I need to handle some OLE/OCP calls, and I'm trying to avoid
    the pain the arse of doing OLE in C.

    Thanks in advance,
    Tom
     
    TBass, Sep 29, 2006
    #1
    1. Advertising

  2. TBass wrote:
    > This is going to be one of those annoyingly stupid questions.
    >
    > I would like to compile a library as a C++ program, but it needs to be
    > able to work with a C program as well. I only want to distribute one
    > .lib file and an H file.
    >
    > I know that I can call a C library from a C++ program. Is it possible
    > to compile a CPP library that can be called from a C program?


    Yes, what you usually do is provide a C interface for every C++ function
    you expect to call:

    int my_Cpp_function(const char*);

    extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
    return my_Cpp_function(cptr);
    }

    Of course, it means that no C++ types can be present in the interface.

    > For the, "why not just make it a C library and stop wasting our time?"
    > question, I need to handle some OLE/OCP calls, and I'm trying to avoid
    > the pain the arse of doing OLE in C.


    Agreed.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 29, 2006
    #2
    1. Advertising

  3. TBass

    TBass Guest

    [snip]
    > Yes, what you usually do is provide a C interface for every C++ function
    > you expect to call:
    >
    > int my_Cpp_function(const char*);
    >
    > extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
    > return my_Cpp_function(cptr);
    > }
    >
    > Of course, it means that no C++ types can be present in the interface.

    [/snip]

    There aren't any. Thanks! That's a big help.
     
    TBass, Sep 29, 2006
    #3
  4. On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
    <> wrote:
    >TBass wrote:
    >> This is going to be one of those annoyingly stupid questions.
    >> I would like to compile a library as a C++ program, but it needs to be
    >> able to work with a C program as well. I only want to distribute one
    >> .lib file and an H file.

    >
    >Yes, what you usually do is provide a C interface for every C++ function
    >you expect to call:
    >
    > int my_Cpp_function(const char*);
    >
    > extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
    > return my_Cpp_function(cptr);
    > }
    >
    >Of course, it means that no C++ types can be present in the interface.


    You can use forward declared C++ types - 'class' renamed to 'struct'
    which may be helpful in that case.

    Best regards,
    Roland Pibinger
     
    Roland Pibinger, Sep 29, 2006
    #4
  5. TBass

    Greg Comeau Guest

    In article <>,
    TBass <> wrote:
    >I would like to compile a library as a C++ program, but it needs to be
    >able to work with a C program as well. I only want to distribute one
    >.lib file and an H file.
    >
    >I know that I can call a C library from a C++ program. Is it possible
    >to compile a CPP library that can be called from a C program?
    >
    >For the, "why not just make it a C library and stop wasting our time?"
    >question, I need to handle some OLE/OCP calls, and I'm trying to avoid
    >the pain the arse of doing OLE in C.


    Check out http://www.comeaucomputing.com/techtalk/#externC

    If your library is a class library, or template library, etc.
    for all practical purposes, don't do that.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Sep 29, 2006
    #5
  6. TBass

    Greg Comeau Guest

    In article <>,
    Roland Pibinger <> wrote:
    >On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
    ><> wrote:
    >>TBass wrote:
    >>> This is going to be one of those annoyingly stupid questions.
    >>> I would like to compile a library as a C++ program, but it needs to be
    >>> able to work with a C program as well. I only want to distribute one
    >>> .lib file and an H file.

    >>
    >>Yes, what you usually do is provide a C interface for every C++ function
    >>you expect to call:
    >>
    >> int my_Cpp_function(const char*);
    >>
    >> extern "C" int my_Cpp_function_C_wrapper(const char* cptr) {
    >> return my_Cpp_function(cptr);
    >> }
    >>
    >>Of course, it means that no C++ types can be present in the interface.

    >
    >You can use forward declared C++ types - 'class' renamed to 'struct'
    >which may be helpful in that case.


    Only if it's truly POD classes.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Sep 29, 2006
    #6
  7. TBass posted:

    > I would like to compile a library as a C++ program, but it needs to be
    > able to work with a C program as well. I only want to distribute one
    > .lib file and an H file.
    >
    > I know that I can call a C library from a C++ program. Is it possible
    > to compile a CPP library that can be called from a C program?



    Here's a sample program:

    /* defs.cpp */

    extern "C" int Func(double const k) { return k / 3.2; }


    /* header.h */

    #ifdef __cplusplus
    extern "C" int Func(double);
    #else
    int Func(double);
    #endif

    /* source1.c */

    #include "header.h"

    int main(void)
    {
    Func(53.6);
    return 0;
    }


    /* source2.cpp */

    #include "header.h"

    int Abc()
    {
    Func(45.8);
    return 0;
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 29, 2006
    #7
  8. On 29 Sep 2006 18:28:10 -0400, (Greg Comeau) wrote:
    >In article <>,
    >Roland Pibinger <> wrote:
    >>You can use forward declared C++ types - 'class' renamed to 'struct'
    >>which may be helpful in that case.

    >
    >Only if it's truly POD classes.


    When you use only pointers it works with non-POD classes too, eg.

    // myclass.h

    struct MyClass;
    typedef struct MyClass MyClass;

    extern "C" {
    MyClass* createMyClass();
    int doSomething (MyClass* m, int i);
    void destroyMyClass (MyClass* m);
    }


    // myclass.cpp

    #include "myclass.h"

    class MyClass {
    public:
    virtual int doSomething (int i) { return i; }
    virtual ~MyClass() {}
    };


    MyClass* createMyClass() { return new MyClass; }
    int doSomething (MyClass* m, int i) {
    return m ? m->doSomething(i) : 0;
    }
    void destroyMyClass (MyClass* m) { delete m; }


    // usage in C program:

    int main() {
    MyClass* m = createMyClass();
    doSomething (m, 7);
    destroyMyClass (m);
    }

    Best regards,
    Roland Pibinger
     
    Roland Pibinger, Sep 30, 2006
    #8
  9. TBass

    Greg Comeau Guest

    In article <>,
    Roland Pibinger <> wrote:
    >On 29 Sep 2006 18:28:10 -0400, (Greg Comeau) wrote:
    >>In article <>,
    >>Roland Pibinger <> wrote:
    >>>You can use forward declared C++ types - 'class' renamed to 'struct'
    >>>which may be helpful in that case.

    >>
    >>Only if it's truly POD classes.

    >
    >When you use only pointers it works with non-POD classes too, eg.
    >
    >// myclass.h
    >
    >struct MyClass;
    >typedef struct MyClass MyClass;
    >
    >extern "C" {
    > MyClass* createMyClass();
    > int doSomething (MyClass* m, int i);
    > void destroyMyClass (MyClass* m);
    >}
    >
    >
    >// myclass.cpp
    >
    >#include "myclass.h"
    >
    >class MyClass {
    >public:
    > virtual int doSomething (int i) { return i; }
    > virtual ~MyClass() {}
    >};
    >
    >
    >MyClass* createMyClass() { return new MyClass; }
    >int doSomething (MyClass* m, int i) {
    > return m ? m->doSomething(i) : 0;
    >}
    >void destroyMyClass (MyClass* m) { delete m; }
    >
    >
    >// usage in C program:
    >
    >int main() {
    > MyClass* m = createMyClass();
    > doSomething (m, 7);
    > destroyMyClass (m);
    >}


    Indeed. I was thinking about him actually accessing
    members of types that don't exist in C though.
    Nope, not impossible, but like the above, stub city et al,
    which can be a real pain to being productive unless
    isolated cases.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Sep 30, 2006
    #9
  10. On 30 Sep 2006 09:22:43 -0400, (Greg Comeau) wrote:
    >Nope, not impossible, but like the above, stub city et al,
    >which can be a real pain to being productive unless
    >isolated cases.


    Yes, that's true. A tool may be helpful: http://www.swig.org/

    Best regards,
    Roland Pibinger
     
    Roland Pibinger, Sep 30, 2006
    #10
    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. Mythran
    Replies:
    0
    Views:
    2,429
    Mythran
    Aug 24, 2004
  2. Alan Ferrandiz [MCT]
    Replies:
    0
    Views:
    447
    Alan Ferrandiz [MCT]
    Sep 11, 2004
  3. Sweep

    Library in library...

    Sweep, Dec 8, 2003, in forum: C++
    Replies:
    1
    Views:
    389
    Jack Klein
    Dec 9, 2003
  4. Replies:
    6
    Views:
    839
    red floyd
    May 10, 2005
  5. iceColdFire

    Static library Vs. Dynamic library

    iceColdFire, May 17, 2005, in forum: C++
    Replies:
    3
    Views:
    17,057
Loading...

Share This Page