*BSD News Article 32577


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msuinfo!agate!howland.reston.ans.net!EU.net!ieunet!news.ieunet.ie!jkh
From: jkh@whisker.hubbard.ie (Jordan Hubbard)
Newsgroups: comp.os.386bsd.questions
Subject: Re: Dynamic C++ object with FreeBSD
Date: 09 Jul 1994 15:20:51 GMT
Organization: Jordan Hubbard
Lines: 116
Distribution: world
Message-ID: <JKH.94Jul9152051@whisker.hubbard.ie>
References: <2vm9ib$kll@news-rocq.inria.fr>
NNTP-Posting-Host: whisker.hubbard.ie
In-reply-to: soulard@alix.inria.fr's message of 9 Jul 1994 13:43:39 GMT

In article <2vm9ib$kll@news-rocq.inria.fr> soulard@alix.inria.fr (Herve Soulard) writes:

   I'm looking for an expert in both gcc and FreeBSD to give me some
   help in implementing dynamically loaded objects.

This is easy..  Here, unpack and try the following shell archive - the
make target will generate an executable called `example', just execute
it.  I suspect your difficulties lay in how you tried to generate the
shared library to dynamically load - the Makefile I've included will
show you how it's done.  I also took the liberty of adding a few
printf()'s to your code to show that it's working.

---

					Jordan


# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	Bar.cxx
#	Bar.hxx
#	Makefile
#	example.cxx
#
echo x - Bar.cxx
sed 's/^X//' >Bar.cxx << 'END-of-Bar.cxx'
X#include "Bar.hxx"
X
XBar::Bar(void) {
X	printf("BAR contructor!\n");
X	i = 0;
X}
X
XBar::~Bar(void) {
X	printf("BAR destructor!\n");
X}
X
Xint Bar::set(int a) {
X	printf("BAR set!  a = %d\n", a);
X	i = a;
X	return i;
X}		
X
XBar* Bar::NEW(void) {
X	printf("BAR new!\n");
X	return new Bar;
X}
END-of-Bar.cxx
echo x - Bar.hxx
sed 's/^X//' >Bar.hxx << 'END-of-Bar.hxx'
Xclass Bar {
X	int i;
Xpublic:
X	Bar(void);
X	~Bar(void);
X
X	virtual int set(int);
X	static Bar* NEW(void);
X};
X
END-of-Bar.hxx
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
Xall: example
X
XCFLAGS += -fpic
X
Xexample: Bar.so
X	$(CXX) example.cxx -o example
X
X# Note that /usr/lib/c++rt0.o is NOT necessary for this example, but including
X# it is a good habit to get into when making C++ shared libraries!  If you're
X# using the standard BSD make macros, simply set CPLUSPLUSLIB before
X# including <bsd.lib.mk> to achieve the same effect.
XBar.so: Bar.o
X	ld -Bshareable /usr/lib/c++rt0.o Bar.o -o Bar.so
X
Xclean:
X	rm -f *.o *.so example
END-of-Makefile
echo x - example.cxx
sed 's/^X//' >example.cxx << 'END-of-example.cxx'
X#include <stdio.h>
X#include <sys/types.h>
X#include <nlist.h>
X
Xextern "C" {
X#include <link.h>
X}
X
X#define PATH "./Bar.so"
X
X#include "Bar.hxx"
X
Xmain()
X{
X    void *addr;
X    void *(*fp)(void);
X    Bar  *bar;
X
X    addr = dlopen(PATH, 1);
X    fp = (void *(*)(void))dlsym(addr, "_NEW__3Bar");
Xprintf("addr = %x, fp = %x\n", addr, fp);
X    bar = (*fp)();
X
X    bar->set(3);
X}
END-of-example.cxx
exit
--
Jordan K. Hubbard	FreeBSD core team	Clams are your friends