For the AIX C++ gurus: creating a C interface to a C++ shard lib

For the AIX C++ gurus: creating a C interface to a C++ shard lib

Post by Edwin Fin » Sat, 17 Nov 2001 22:09:30



I have created -- using xlC -- a shared library containing shared C++
objects.
One of these objects presents an extern "C" interface that wraps the C++
classes.
Programs compiled with xlc will link with the shared library with both xlc
and xlC
linking, but only the xlC linker works. We want to ship these libraries to
customers
who don't necessarily have the xlC compiler/linker.

Can anyone tell me how to produce a C wrapper for a C++ class, such that the
C
wrapper is an the same shared library as the C++ class?

Thanks in advance for any help.

--
Regards,

Edwin Fine

 
 
 

For the AIX C++ gurus: creating a C interface to a C++ shard lib

Post by Paul Pluzhniko » Sun, 18 Nov 2001 14:32:08



Quote:> I have created -- using xlC -- a shared library containing shared C++
objects.
> One of these objects presents an extern "C" interface that wraps the C++
classes.
> Programs compiled with xlc will link with the shared library with both xlc
> and xlC linking, but only the xlC linker works. We want to ship these
libraries to
> customers who don't necessarily have the xlC compiler/linker.

Presumably, linking with xlc does not work because
xlc does not add libC.a to the link line, with the end result
of e.g. "operator new(size_t)" coming out undefined.

You can link libC.a statically into your shared library
(with -bnso option).

If your problem is something else, post more details:
compile line, link line (for the shared library),
xlc link line for the final exe, errors you get, etc.

 
 
 

For the AIX C++ gurus: creating a C interface to a C++ shard lib

Post by Edwin Fin » Wed, 21 Nov 2001 02:10:38


Your answer gave me an idea, so I statically linked
libC.a into the executable that was having a problem with
the shared library in question.

It worked!

Thank you!
--
Regards,

Edwin Fine




> > I have created -- using xlC -- a shared library containing shared C++
> objects.
> > One of these objects presents an extern "C" interface that wraps the C++
> classes.
> > Programs compiled with xlc will link with the shared library with both
xlc
> > and xlC linking, but only the xlC linker works. We want to ship these
> libraries to
> > customers who don't necessarily have the xlC compiler/linker.

> Presumably, linking with xlc does not work because
> xlc does not add libC.a to the link line, with the end result
> of e.g. "operator new(size_t)" coming out undefined.

> You can link libC.a statically into your shared library
> (with -bnso option).

> If your problem is something else, post more details:
> compile line, link line (for the shared library),
> xlc link line for the final exe, errors you get, etc.

 
 
 

For the AIX C++ gurus: creating a C interface to a C++ shard lib

Post by Gary R. Hoo » Wed, 21 Nov 2001 06:36:59



> I have created -- using xlC -- a shared library containing shared C++
> objects.
> One of these objects presents an extern "C" interface that wraps the C++
> classes.
> Programs compiled with xlc will link with the shared library with both xlc
> and xlC
> linking, but only the xlC linker works. We want to ship these libraries to
> customers
> who don't necessarily have the xlC compiler/linker.

> Can anyone tell me how to produce a C wrapper for a C++ class, such that the
> C
> wrapper is an the same shared library as the C++ class?

This should be no more difficult than writing 'extern "C" {}' code
around
your C interface routines, and compiling and linking them all into a
single
shared module.  The extern "C" construct creates regular c linkage
(and symbol naming) for use by languages other than C++.

The real question is what is your command line for building the module?
On AIX dependent modules are named at link time, and even if your
customer
doesn't use xlC, your module already contains the dependency upon
libC.a/etc
and will be properly resolved.

In general, taking both C and C++ object code, your link command
should look something like

xlC -qmkshrobj -o foo.so cobj.o cppobj.o .... -L<whatever> -l<whatever>
...

Then examine the loader section of the module with dump -HTv and ensure
that
foo.so requires libC.a(shr.o) and others.

FYI you'll also want to specify a minimum level of the C++ runtime
for your customers, forcing them to update to current levels if
necessary.

--
Gary R. Hook / AIX PartnerWorld for Developers / These opinions are MINE
________________________________________________________________________

 
 
 

1. Problem creating C++ shared libs on AIX

Please forgive me if this has been asked before, but this is my
first visit to this newsgroup.

I'm trying to build a simple shared library using C++ under IBM's
AIX 4.2.1 on an RS 6000 machine and have been unsuccessful so far.

The examples on pages 19-20 of Kevin E. Leininger's "AIX 6000
Developer's Took Kit" book have been the basis for my first
effort.  I can successfully build the C example in the book
but am having problems building a C++ shared library.

The source code of the library (common.C) and header (common.h) looks
like this:

int common();

#include <iostream.h>
#include "common.h"
int common()
{
   cout << "This is a shared library function\n";
   return 0;

It was successfully compiled with the command:

xlC -c common.C -o common.o

The export file, common.exp, looks like this:

#! ./commonobj
common

The link command looks like this:

ld -H512 -T512 -bglink:/lib/glink.o -b export:common.exp -bM:SRE -ocommonobj -lC common.o

The result of this operation is the following error message:

ld: 0711-327 WARNING: Entry point not found: __start
ld: 0711-319 WARNING: Exported symbol not defined: common
ld: 0711-244 ERROR: No csects or exported symbols have been saved.

Can anyone tell what the problem might be?

THANKS IN ADVANCE!

Martin Sagara

2. How to install USR V-everything on a leased Line?

3. C++ linking problem creating a static lib from another static lib.

4. USER AGENT

5. C++ compilers, NIH C++ class lib

6. Keyboard is locking up...

7. Text/Cursor/terminal Uner Interface lib for C/C++ & Linux

8. advice for getting started

9. TCPee Lib v0.6, yet another C++ TCP/IP lib

10. c++ in a shared lib on AIX 4.3.3

11. Creating Custom Class Templates in C++ on AIX

12. these 3 questions from my C++ E-mail group, and my reply to C++ group

13. c++ IDE like Microsoft's Visual C++, is there one