C++ code with C-style interface for a library to be used in C++ and C?

Discussion in 'C++' started by Koen, Sep 19, 2003.

  1. Koen

    Koen Guest

    Hi!

    I have a question about building and then using libraries containing
    C++ code.

    Let's say I have some C++ code and a .cpp file with 1 function that
    uses some other C++ code / classes etc... Also, any possible exception
    is handled within the function itself.

    In code:

    MyModule.h
    ----------
    #ifndef MYMODULE_H
    #define MYMODULE_H

    extern int Test(float inParam1,float inParam2,float* outResult);

    #endif // #ifndef MYMODULE_H

    MyModule.cpp
    ------------
    #include "MyModule.h"
    #include "MyClasses.h" // contains MyClassA and MyClassB

    int Test(float inParam1,float inParam2,float* outResult)
    {
    int theResult = 0;
    try
    {
    MyClassA a;
    a.Setup(inParam1);
    MyClassB b;
    b.Setup(inParam2);
    *outResult = a.Process(b);
    }
    catch (...)
    {
    theResult = -1;
    }
    return theResult;
    }

    Now, I would like to build a library that is callable from C++ AND
    from C that exposes the functionality of that Test function (I just
    gave an example with 1 single function, but in practice there are
    more).

    Currently, when I build the library, it is compiled using the C++
    compiler (of course, since I really use C++ classes and so on), and I
    can use the library from a C++ program (.cpp file with main), as
    should...

    But I can't seem to find out how to make that function accessible to a
    C program (.c file with main). I get an error like "unresolved
    external symbol _Test", and I know that it probably has something to
    do with the name mangling in C++ being different from C...
    Considering the fact that the *interface* of the library does not
    contain any C++ specific things, and handles all possible exceptions
    internally, it should be possible to use it in C too, right?

    Can someone please explain me how to do that?
    Thanks in advance!

    Koen

    PS
    If someone wants the test code, I can post/send them on request.
     
    Koen, Sep 19, 2003
    #1
    1. Advertising

  2. Koen

    Koen Guest

    "Koen" <> wrote in message
    news:bkf0gh$smr$...
    > Hi!
    >
    > I have a question about building and then using libraries containing
    > C++ code.
    >
    > Let's say I have some C++ code and a .cpp file with 1 function that
    > uses some other C++ code / classes etc... Also, any possible

    exception
    > is handled within the function itself.
    >
    > In code:
    >
    > MyModule.h
    > ----------
    > #ifndef MYMODULE_H
    > #define MYMODULE_H
    >
    > extern int Test(float inParam1,float inParam2,float* outResult);
    >
    > #endif // #ifndef MYMODULE_H


    OK. Seems like all I needed to do was this:

    #ifndef MYMODULE_H
    #define MYMODULE_H

    #ifdef __cplusplus
    extern "C" {
    #endif

    int Test(float inParam1,float inParam2,float* outResult);

    #ifdef __cplusplus
    }
    #endif

    #endif // #ifndef MYMODULE_H


    Only thing I'm not sure of anymore is whether I should still keep the
    "extern" in front of my function (so that also in the C case there is
    an "extern"):

    extern int Test(float inParam1,float inParam2,float* outResult);

    Does that still have any use?

    Koen
     
    Koen, Sep 19, 2003
    #2
    1. Advertising

  3. Re: C++ code with C-style interface for a library to be used in C++ andC?

    Koen escribió:

    > But I can't seem to find out how to make that function accessible to a
    > C program (.c file with main). I get an error like "unresolved
    > external symbol _Test", and I know that it probably has something to
    > do with the name mangling in C++ being different from C...


    Declare your function as extern "C".

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Sep 19, 2003
    #3
  4. Koen

    Mike Wahler Guest

    "Koen" <> wrote in message
    news:bkf439$tr2$...
    > "Koen" <> wrote in message
    > news:bkf0gh$smr$...
    > > Hi!
    > >
    > > I have a question about building and then using libraries containing
    > > C++ code.
    > >
    > > Let's say I have some C++ code and a .cpp file with 1 function that
    > > uses some other C++ code / classes etc... Also, any possible

    > exception
    > > is handled within the function itself.
    > >
    > > In code:
    > >
    > > MyModule.h
    > > ----------
    > > #ifndef MYMODULE_H
    > > #define MYMODULE_H
    > >
    > > extern int Test(float inParam1,float inParam2,float* outResult);
    > >
    > > #endif // #ifndef MYMODULE_H

    >
    > OK. Seems like all I needed to do was this:
    >
    > #ifndef MYMODULE_H
    > #define MYMODULE_H
    >
    > #ifdef __cplusplus
    > extern "C" {
    > #endif
    >
    > int Test(float inParam1,float inParam2,float* outResult);
    >
    > #ifdef __cplusplus
    > }
    > #endif
    >
    > #endif // #ifndef MYMODULE_H
    >
    >
    > Only thing I'm not sure of anymore is whether I should still keep the
    > "extern" in front of my function (so that also in the C case there is
    > an "extern"):
    >
    > extern int Test(float inParam1,float inParam2,float* outResult);


    In both C and C++, functions are 'extern' by default,
    and need not be qualified as such.

    The use of 'extern' with 'extern "C"' has a special
    purpose meaning, used for the interlanguage interface.

    >
    > Does that still have any use?


    No, it never did. :)

    -Mike
     
    Mike Wahler, Sep 19, 2003
    #4
  5. Koen

    Koen Guest

    "Mike Wahler" <> wrote in message
    news:fhHab.8773$...

    > In both C and C++, functions are 'extern' by default,
    > and need not be qualified as such.
    >
    > The use of 'extern' with 'extern "C"' has a special
    > purpose meaning, used for the interlanguage interface.
    >
    > >
    > > Does that still have any use?

    >
    > No, it never did. :)


    OK. Thanks for the info!
    Koen
     
    Koen, Sep 19, 2003
    #5
  6. Koen

    Jack Klein Guest

    On Fri, 19 Sep 2003 15:49:33 +0200, "Koen" <> wrote in
    comp.lang.c++:

    > Hi!
    >
    > I have a question about building and then using libraries containing
    > C++ code.
    >
    > Let's say I have some C++ code and a .cpp file with 1 function that
    > uses some other C++ code / classes etc... Also, any possible exception
    > is handled within the function itself.
    >
    > In code:
    >
    > MyModule.h
    > ----------
    > #ifndef MYMODULE_H
    > #define MYMODULE_H
    >
    > extern int Test(float inParam1,float inParam2,float* outResult);
    >
    > #endif // #ifndef MYMODULE_H
    >
    > MyModule.cpp
    > ------------
    > #include "MyModule.h"
    > #include "MyClasses.h" // contains MyClassA and MyClassB
    >
    > int Test(float inParam1,float inParam2,float* outResult)
    > {
    > int theResult = 0;
    > try
    > {
    > MyClassA a;
    > a.Setup(inParam1);
    > MyClassB b;
    > b.Setup(inParam2);
    > *outResult = a.Process(b);
    > }
    > catch (...)
    > {
    > theResult = -1;
    > }
    > return theResult;
    > }
    >
    > Now, I would like to build a library that is callable from C++ AND
    > from C that exposes the functionality of that Test function (I just
    > gave an example with 1 single function, but in practice there are
    > more).
    >
    > Currently, when I build the library, it is compiled using the C++
    > compiler (of course, since I really use C++ classes and so on), and I
    > can use the library from a C++ program (.cpp file with main), as
    > should...
    >
    > But I can't seem to find out how to make that function accessible to a
    > C program (.c file with main). I get an error like "unresolved
    > external symbol _Test", and I know that it probably has something to
    > do with the name mangling in C++ being different from C...
    > Considering the fact that the *interface* of the library does not
    > contain any C++ specific things, and handles all possible exceptions
    > internally, it should be possible to use it in C too, right?
    >
    > Can someone please explain me how to do that?
    > Thanks in advance!
    >
    > Koen
    >
    > PS
    > If someone wants the test code, I can post/send them on request.
    >


    You probably can't use C++ code that does things like throwing and
    catching exceptions from inside a C program.

    While this is implementation specific, it is often necessary on many
    platforms for a program containing mixed C and C++ object modules to
    have the start-up and main() in C++. It is quite possible that the C
    environment created by the C compiler for a C executable will not have
    appropriate support for C++ only features such as exceptions.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 20, 2003
    #6
    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. PerfectDayToChaseTornados

    CIM used for Web Service interface

    PerfectDayToChaseTornados, Aug 11, 2005, in forum: Java
    Replies:
    0
    Views:
    567
    PerfectDayToChaseTornados
    Aug 11, 2005
  2. Casey Hawthorne
    Replies:
    1
    Views:
    745
    Arne Vajhøj
    Mar 18, 2009
  3. Markus Schaber
    Replies:
    1
    Views:
    645
    Detlev Offenbach
    Mar 27, 2011
  4. Ken Varn
    Replies:
    0
    Views:
    506
    Ken Varn
    Apr 26, 2004
  5. Ted Byers
    Replies:
    23
    Views:
    441
    Peter J. Holzer
    Nov 15, 2008
Loading...

Share This Page