Re: Wrapping C++ with C
From: Paul Pluzhnikov (ppluzhnikov-nsp_at_charter.net)
Date: 09/12/04
- Previous message: Paul Pluzhnikov: "Re: KDevelop/C++ question"
- In reply to: Roger Leigh: "Wrapping C++ with C"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 12 Sep 2004 08:31:24 -0700
Roger Leigh <${roger}@invalid.whinlatter.uklinux.net.invalid> writes:
> For several reasons, I'd like to make use of C++ features such as
> templated functions and STL lists and maps in a C shared library.
> However, from the outside I'd like it to appear as a regular C library
> with a C API and not requiring a C++ compiler for linking. Is this
> possible?
Yes. But there are many gotcha's.
> I believe all I need to do is write the code as C++ code with the C
> wrapper functions declared as extern "C". Is this correct?
That's all you have to do IFF your std::list and std::map are
implemented entirely as templates, and do not introduce dependency
on C++ runtime libraries for your compiler. This appears to be the
case for g++ 2.95 and 3.4.2; but it is not the case for Sun Workshop,
nor for HP aCC.
If there are external C++ dependencies, you'll have to carefully
arrange for them to disappear by linking the missing C++ symbols
from the compiler runtime into your library *statically* [1] (so that
your clients could be "plain C" programs), and then hiding these
symbols (because your client may be using a different version of
the same compiler, and you would not want to have your "imported"
symbols clash with his imported symbols).
[1] This assumes that you have an archive version of said runtime
library.
A further complication is that the archive version of the compiler
runtime is often not compiled as position-independent, and thus is
not suitable for inclusion into a shared library.
> Are there any portability or linking issues to be aware of?
You bet.
Cheers,
-- In order to understand recursion you must first understand recursion. Remove /-nsp/ for email.
- Previous message: Paul Pluzhnikov: "Re: KDevelop/C++ question"
- In reply to: Roger Leigh: "Wrapping C++ with C"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|