C++ library access from C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TBass

    #1

    C++ library access from C

    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

  • Victor Bazarov

    #2
    Re: C++ library access from C

    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(cons t 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


    Comment

    • TBass

      #3
      Re: C++ library access from C

      [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(cons t 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.

      Comment

      • Roland Pibinger

        #4
        Re: C++ library access from C

        On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
        <v.Abazarov@com Acast.netwrote:
        >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(cons t 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

        Comment

        • Greg Comeau

          #5
          Re: C++ library access from C

          In article <1159548059.941 307.147180@e3g2 000cwe.googlegr oups.com>,
          TBass <tbj@automatedd esign.comwrote:
          >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?

          Comment

          • Greg Comeau

            #6
            Re: C++ library access from C

            In article <451d629c.11817 99@news.utanet. at>,
            Roland Pibinger <rpbg123@yahoo. comwrote:
            >On Fri, 29 Sep 2006 12:50:58 -0400, "Victor Bazarov"
            ><v.Abazarov@co mAcast.netwrote :
            >>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(cons t 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?

            Comment

            • Frederick Gotham

              #7
              Re: C++ library access from C

              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

              Comment

              • Roland Pibinger

                #8
                Re: C++ library access from C

                On 29 Sep 2006 18:28:10 -0400, comeau@panix.co m (Greg Comeau) wrote:
                >In article <451d629c.11817 99@news.utanet. at>,
                >Roland Pibinger <rpbg123@yahoo. comwrote:
                >>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

                Comment

                • Greg Comeau

                  #9
                  Re: C++ library access from C

                  In article <451e2505.26212 79@news.utanet. at>,
                  Roland Pibinger <rpbg123@yahoo. comwrote:
                  >On 29 Sep 2006 18:28:10 -0400, comeau@panix.co m (Greg Comeau) wrote:
                  >>In article <451d629c.11817 99@news.utanet. at>,
                  >>Roland Pibinger <rpbg123@yahoo. comwrote:
                  >>>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?

                  Comment

                  • Roland Pibinger

                    #10
                    Re: C++ library access from C

                    On 30 Sep 2006 09:22:43 -0400, comeau@panix.co m (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

                    Comment

                    Working...