Standalone C wrapper to C++ library

D

deech

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

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.
 
D

deech

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

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.
 
J

James Kuyper

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.
 
I

Ian Collins

deech said:
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.
 
J

James Kuyper

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

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.
 
G

glen herrmannsfeldt

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
 
J

James Kuyper

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.
 
G

glen herrmannsfeldt

James Kuyper said:
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.

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
 
J

James Kuyper

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top