Standalone C wrapper to C++ library

Discussion in 'C Programming' started by deech, May 7, 2013.

  1. deech

    deech Guest

    Hi all,
    I am not an experienced C developer and would like to know if there are good resources for, or better yet real-world examples of, wrapping a C++ library in C such that application written using it don't need to have the underlying C++ library.

    The best resource [1] I've found shows me how to create a shared library which seems to requires the C++ library to be installed.

    Thanks for your help!
    -deech

    [1] http://losinggeneration.homelinux.org/2012/08/08/c-bindings-for-c/
    deech, May 7, 2013
    #1
    1. Advertising

  2. deech

    Öö Tiib Guest

    On Wednesday, 8 May 2013 01:51:45 UTC+3, deech wrote:
    > Hi all,
    >
    > I am not an experienced C developer and would like to know if there are
    > good resources for, or better yet real-world examples of, wrapping a
    > C++ library in C such that application written using it don't need to
    > have the underlying C++ library.
    >
    > The best resource [1] I've found shows me how to create a shared
    > library which seems to requires the C++ library to be installed.


    Yes ... but how exactly you imagine it using something that is not
    present? Application can load shared library dynamically and if it
    is not present (the dynamic loading failed) then tell that it is
    unavailable/not installed ... but actual usage ... that should
    feel impossible even for inexperienced.
    Öö Tiib, May 8, 2013
    #2
    1. Advertising

  3. deech

    deech Guest

    I think I wasn't clear. I meant that I want to be able to distribute the application written with the c wrapper as a fat executable so that the user can run it without having to install anything else.

    Thanks for clarifying.
    -deech

    On Tuesday, May 7, 2013 7:09:44 PM UTC-5, Öö Tiib wrote:
    > On Wednesday, 8 May 2013 01:51:45 UTC+3, deech wrote:
    >
    > > Hi all,

    >
    > >

    >
    > > I am not an experienced C developer and would like to know if there are

    >
    > > good resources for, or better yet real-world examples of, wrapping a

    >
    > > C++ library in C such that application written using it don't need to

    >
    > > have the underlying C++ library.

    >
    > >

    >
    > > The best resource [1] I've found shows me how to create a shared

    >
    > > library which seems to requires the C++ library to be installed.

    >
    >
    >
    > Yes ... but how exactly you imagine it using something that is not
    >
    > present? Application can load shared library dynamically and if it
    >
    > is not present (the dynamic loading failed) then tell that it is
    >
    > unavailable/not installed ... but actual usage ... that should
    >
    > feel impossible even for inexperienced.
    deech, May 8, 2013
    #3
  4. deech

    James Kuyper Guest

    On 05/07/2013 06:51 PM, deech wrote:
    > Hi all, I am not an experienced C developer and would like to know if
    > there are good resources for, or better yet real-world examples of,
    > wrapping a C++ library in C such that application written using it
    > don't need to have the underlying C++ library.


    There's a fundamental conflict built into your concept. You'll have to
    resolve that conflict before you can do anything useful. Do you want to
    wrap the C++ library, or replace it? A wrapper, by definition, calls the
    function that it is a wrapper for; you can't have a working wrapper if
    you don't have a working copy of the wrapped function.

    > The best resource [1] I've found shows me how to create a shared
    > library which seems to requires the C++ library to be installed.


    Yes, that's normally the case. If that's unacceptable, then your only
    alternative is to replace the library, rather than provide wrappers for
    it. In general, that's going to involve a lot more work.
    --
    James Kuyper
    James Kuyper, May 8, 2013
    #4
  5. deech

    Ian Collins Guest

    deech wrote:
    > I think I wasn't clear. I meant that I want to be able to distribute
    > the application written with the c wrapper as a fat executable so
    > that the user can run it without having to install anything else.


    Please don't top-post!

    In general, if the library is a dynamic library you can't include it in
    a fat executable. If it is a static library, then yes you can include it.

    --
    Ian Collins
    Ian Collins, May 8, 2013
    #5
  6. deech

    James Kuyper Guest

    On 05/07/2013 10:19 PM, deech wrote:
    > I think I wasn't clear. I meant that I want to be able to distribute
    > the application written with the c wrapper as a fat executable so
    > that the user can run it without having to install anything else.
    >
    > Thanks for clarifying.
    > -deech
    >
    > On Tuesday, May 7, 2013 7:09:44 PM UTC-5, �� Tiib wrote:
    >> On Wednesday, 8 May 2013 01:51:45 UTC+3, deech wrote:
    >>> Hi all,
    >>>
    >>> I am not an experienced C developer and would like to know if
    >>> there are
    >>> good resources for, or better yet real-world examples of,
    >>> wrapping a
    >>> C++ library in C such that application written using it don't
    >>> need to
    >>> have the underlying C++ library.
    >>>
    >>> The best resource [1] I've found shows me how to create a shared
    >>> library which seems to requires the C++ library to be
    >>> installed.


    Yes, the C++ libraries you're wrapping must be present when you create
    your program. Whether they also need to be present when the program is
    executed depends upon how your code is linked. The way to link it to
    create a "fat executable" is different on each of the operating systems
    I'm familiar with, so you'll need to identify which one you're using.
    Once you have, the best place to get an answer to your question will be
    a forum devoted to that operating system. comp.lang.c isn't that forum,
    no matter which OS you're using.
    --
    James Kuyper
    James Kuyper, May 8, 2013
    #6
  7. James Kuyper <> wrote:
    > On 05/07/2013 06:51 PM, deech wrote:
    >> Hi all, I am not an experienced C developer and would like to know if
    >> there are good resources for, or better yet real-world examples of,
    >> wrapping a C++ library in C such that application written using it
    >> don't need to have the underlying C++ library.


    > There's a fundamental conflict built into your concept. You'll have to
    > resolve that conflict before you can do anything useful. Do you want to
    > wrap the C++ library, or replace it? A wrapper, by definition, calls the
    > function that it is a wrapper for; you can't have a working wrapper if
    > you don't have a working copy of the wrapped function.


    Well, the wrapped function doesn't have to call (directly or
    indirectly) C++ library routines. Most will, but some might not, and
    it might not be so hard to change the calls, for example to C library
    calls instead.

    >> The best resource [1] I've found shows me how to create a shared
    >> library which seems to requires the C++ library to be installed.


    > Yes, that's normally the case. If that's unacceptable, then your only
    > alternative is to replace the library, rather than provide wrappers for
    > it. In general, that's going to involve a lot more work.


    In general, but maybe not always.

    -- glen
    glen herrmannsfeldt, May 8, 2013
    #7
  8. deech

    James Kuyper Guest

    On 05/07/2013 11:49 PM, glen herrmannsfeldt wrote:
    > James Kuyper <> wrote:
    >> On 05/07/2013 06:51 PM, deech wrote:
    >>> Hi all, I am not an experienced C developer and would like to know if
    >>> there are good resources for, or better yet real-world examples of,
    >>> wrapping a C++ library in C such that application written using it
    >>> don't need to have the underlying C++ library.

    >
    >> There's a fundamental conflict built into your concept. You'll have to
    >> resolve that conflict before you can do anything useful. Do you want to
    >> wrap the C++ library, or replace it? A wrapper, by definition, calls the
    >> function that it is a wrapper for; you can't have a working wrapper if
    >> you don't have a working copy of the wrapped function.

    >
    > Well, the wrapped function doesn't have to call (directly or
    > indirectly) C++ library routines. Most will, but some might not, ...


    Huh? It doesn't matter whether the wrapped function calls other C++
    library routines; the fact that the wrapped function is itself a C++
    library routine is sufficient for there to be a problem. If the library
    containing the wrapped function is not available, the wrapping function
    can't work.

    ....
    > ... and
    > it might not be so hard to change the calls, for example to C library
    > calls instead.


    Well, first he'd have to write C library functions corresponding to the
    C++ library functions he wanted to make wrappers for. He didn't indicate
    which functions he was thinking of wrapping; he didn't even mention
    which library they were from, so we have no way of knowing how difficult
    of a task that might be. However, if writing equivalent C code were a
    trivial task, I doubt he'd be thinking about using wrappers.

    Later messages on another branch of this thread have clarified that his
    issue is about the distinction between what, in a unix context, would be
    described as static vs. dynamic linking. If the wrapped function is part
    of a shared-object C++ library that is dynamically linked, it's a
    problem if that library is not installed on on the machine where the
    program will be executed. What he describes as a "fat executable"
    corresponds to linking in the library statically. The same issue comes
    up in other operating systems, but the description above might need
    appropriate modifications.
    --
    James Kuyper
    James Kuyper, May 8, 2013
    #8
  9. James Kuyper <> wrote:
    > On 05/07/2013 11:49 PM, glen herrmannsfeldt wrote:
    >> James Kuyper <> wrote:
    >>> On 05/07/2013 06:51 PM, deech wrote:
    >>>> Hi all, I am not an experienced C developer and would like to know if
    >>>> there are good resources for, or better yet real-world examples of,
    >>>> wrapping a C++ library in C such that application written using it
    >>>> don't need to have the underlying C++ library.


    (snip)
    >> Well, the wrapped function doesn't have to call (directly or
    >> indirectly) C++ library routines. Most will, but some might not, ...


    > Huh? It doesn't matter whether the wrapped function calls other C++
    > library routines; the fact that the wrapped function is itself a C++
    > library routine is sufficient for there to be a problem. If the library
    > containing the wrapped function is not available, the wrapping function
    > can't work.


    OK, I was reading it as "a C++ library", not "the C++ library."
    Consider someone writing "a C++ library for numerical integration."
    Normally, one won't need to do I/O, and often won't need any
    memory allocation, other than local (stack) variables.
    In that case, it isn't hard to avoid calling any library routines.

    >> ... and
    >> it might not be so hard to change the calls, for example to C library
    >> calls instead.


    One might, for example, call the C malloc() and free() even in C++.

    > Well, first he'd have to write C library functions corresponding to the
    > C++ library functions he wanted to make wrappers for. He didn't indicate
    > which functions he was thinking of wrapping; he didn't even mention
    > which library they were from, so we have no way of knowing how difficult
    > of a task that might be. However, if writing equivalent C code were a
    > trivial task, I doubt he'd be thinking about using wrappers.


    (snip on dynamic linking)

    -- glen
    glen herrmannsfeldt, May 8, 2013
    #9
  10. deech

    James Kuyper Guest

    On 05/08/2013 02:23 PM, glen herrmannsfeldt wrote:
    > James Kuyper <> wrote:
    >> On 05/07/2013 11:49 PM, glen herrmannsfeldt wrote:
    >>> James Kuyper <> wrote:
    >>>> On 05/07/2013 06:51 PM, deech wrote:
    >>>>> Hi all, I am not an experienced C developer and would like to know if
    >>>>> there are good resources for, or better yet real-world examples of,
    >>>>> wrapping a C++ library in C such that application written using it
    >>>>> don't need to have the underlying C++ library.

    >
    > (snip)
    >>> Well, the wrapped function doesn't have to call (directly or
    >>> indirectly) C++ library routines. Most will, but some might not, ...

    >
    >> Huh? It doesn't matter whether the wrapped function calls other C++
    >> library routines; the fact that the wrapped function is itself a C++
    >> library routine is sufficient for there to be a problem. If the library
    >> containing the wrapped function is not available, the wrapping function
    >> can't work.

    >
    > OK, I was reading it as "a C++ library", not "the C++ library."


    So was I. He might have been referring to a function from the C++
    standard library, or he might not. So long as the library containing the
    wrapped function is the same one that he didn't want the wrapping
    function to need, the issues are precisely the same either way. I can
    make sense of your comments by assuming that you thought they were two
    different libraries - is that how you interpreted it?

    > Consider someone writing "a C++ library for numerical integration."
    > Normally, one won't need to do I/O, and often won't need any
    > memory allocation, other than local (stack) variables.
    > In that case, it isn't hard to avoid calling any library routines.


    True, but if that library included a function template named
    numint::Trapezoid_Rule<T>(), and someone wrote a C function named
    trapezoid_rule_double() as a wrapper for numint::Trapezoid_Rule<double>,
    the wrapper still wouldn't work if the numerical integration library
    were unavailable, even if numint::Trapezoid_Rule<double> doesn't call
    any library routines. It's the absence of the numerical integration
    library that matters, not the details of what
    numint::Trapezoid_Rule<double> would have done if the library had been
    present.
    James Kuyper, May 8, 2013
    #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. Ian
    Replies:
    3
    Views:
    633
    red floyd
    Oct 18, 2003
  2. Noah Roberts

    standalone xml library?

    Noah Roberts, Mar 24, 2005, in forum: C++
    Replies:
    6
    Views:
    1,372
    EventHelix.com
    Mar 25, 2005
  3. Francesco Bochicchio
    Replies:
    6
    Views:
    467
    Thomas Heller
    Jul 4, 2003
  4. Thomas Hafner
    Replies:
    2
    Views:
    139
    Thomas Hafner
    Apr 21, 2007
  5. Fahim Patel
    Replies:
    1
    Views:
    119
    Quintus
    Apr 22, 2014
Loading...

Share This Page