Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Standalone C wrapper to C++ library

63 views
Skip to first unread message

deech

unread,
May 7, 2013, 6:51:45 PM5/7/13
to
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/

Öö Tiib

unread,
May 7, 2013, 8:09:44 PM5/7/13
to
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

unread,
May 7, 2013, 10:19:40 PM5/7/13
to
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

James Kuyper

unread,
May 7, 2013, 10:19:55 PM5/7/13
to
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

Ian Collins

unread,
May 7, 2013, 10:37:10 PM5/7/13
to
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

James Kuyper

unread,
May 7, 2013, 10:51:21 PM5/7/13
to
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

glen herrmannsfeldt

unread,
May 7, 2013, 11:49:56 PM5/7/13
to
James Kuyper <james...@verizon.net> 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

James Kuyper

unread,
May 8, 2013, 7:12:37 AM5/8/13
to
On 05/07/2013 11:49 PM, glen herrmannsfeldt wrote:
> James Kuyper <james...@verizon.net> 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

glen herrmannsfeldt

unread,
May 8, 2013, 2:23:05 PM5/8/13
to
James Kuyper <james...@verizon.net> wrote:
> On 05/07/2013 11:49 PM, glen herrmannsfeldt wrote:
>> James Kuyper <james...@verizon.net> 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

James Kuyper

unread,
May 8, 2013, 3:22:01 PM5/8/13
to
On 05/08/2013 02:23 PM, glen herrmannsfeldt wrote:
> James Kuyper <james...@verizon.net> wrote:
>> On 05/07/2013 11:49 PM, glen herrmannsfeldt wrote:
>>> James Kuyper <james...@verizon.net> 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.

0 new messages