dynamically loading C++ classes

Does anyone know if it is possible to export C++ classes for dynamic
instantiation? I have had no problem creating and using dynamic libraries
if they have C functions or export “C” in .cpp files. I have not been able
to find anything on the subject in the newsgroups or on the web. If anyone
could provide any information or examples it would be greatly appreciated.

Thanks,

Kevin

Nothing much to say about it… You can use C++ classes in dynamic libraries
the same way you use C functions. I do it all the time.
Markus

“Kevin Hykin” <kevin.hykin@bepco.com> wrote in message
news:9gnmh5$t7b$1@inn.qnx.com

Does anyone know if it is possible to export C++ classes for dynamic
instantiation? I have had no problem creating and using dynamic libraries
if they have C functions or export “C” in .cpp files. I have not been
able
to find anything on the subject in the newsgroups or on the web. If
anyone
could provide any information or examples it would be greatly appreciated.

Thanks,

Kevin

“Markus Loffler” <loffler@ces.clemson.edu> wrote in message
news:9gnsaf$3a6$1@inn.qnx.com

Nothing much to say about it… You can use C++ classes in dynamic
libraries
the same way you use C functions. I do it all the time.
Markus

Thanks, if you could elaborate on how I could do this in the example
below or refer me to a good example of how to do it, I would really
appreciate it.

From my experience using C and extern “C” dynamic libraries, I believe
that If I declare a class called:

class Component1 : Component
{
public:
some code here;
private
some code and member variables here;
};

in a file called comp.h and I defined it in a class called comp.cpp and then
compile to comp.o. I then create a dynamic library using:

qcc -g -shared -W1,-soname,comp.so.0 -o comp.so.0.0 comp.o -lc

and end up with a library file called comp.so. How do you get the reference
to the class so I can use it? I have tried to put a extern “C” function
that dynamically instantiates the class but Neutrino definitely does not
like that. All I want to do is use a base class reference and return and
instantiated subclass object to it, or even better, instantiate one in the
code. Is there something I am missing?

Thanks for your help,

Kevin

“Kevin Hykin” <> kevin.hykin@bepco.com> > wrote in message
news:9gnmh5$t7b$> 1@inn.qnx.com> …
Does anyone know if it is possible to export C++ classes for dynamic
instantiation? I have had no problem creating and using dynamic
libraries
if they have C functions or export “C” in .cpp files. I have not been
able
to find anything on the subject in the newsgroups or on the web. If
anyone
could provide any information or examples it would be greatly
appreciated.

Thanks,

Kevin
\

First, call your dynamic lib “libcomp.so”
The, just include “comp.h” and link to “libcomp.so”
The latter you do with linker option -Bdynamic -lcomp
Markus



“Kevin Hykin” <kevin.hykin@bepco.com> wrote in message
news:9go0ja$5q2$1@inn.qnx.com

“Markus Loffler” <> loffler@ces.clemson.edu> > wrote in message
news:9gnsaf$3a6$> 1@inn.qnx.com> …
Nothing much to say about it… You can use C++ classes in dynamic
libraries
the same way you use C functions. I do it all the time.
Markus


Thanks, if you could elaborate on how I could do this in the example
below or refer me to a good example of how to do it, I would really
appreciate it.

From my experience using C and extern “C” dynamic libraries, I believe
that If I declare a class called:

class Component1 : Component
{
public:
some code here;
private
some code and member variables here;
};

in a file called comp.h and I defined it in a class called comp.cpp and
then
compile to comp.o. I then create a dynamic library using:

qcc -g -shared -W1,-soname,comp.so.0 -o comp.so.0.0 comp.o -lc

and end up with a library file called comp.so. How do you get the
reference
to the class so I can use it? I have tried to put a extern “C” function
that dynamically instantiates the class but Neutrino definitely does not
like that. All I want to do is use a base class reference and return and
instantiated subclass object to it, or even better, instantiate one in the
code. Is there something I am missing?

Thanks for your help,

Kevin

“Kevin Hykin” <> kevin.hykin@bepco.com> > wrote in message
news:9gnmh5$t7b$> 1@inn.qnx.com> …
Does anyone know if it is possible to export C++ classes for dynamic
instantiation? I have had no problem creating and using dynamic
libraries
if they have C functions or export “C” in .cpp files. I have not
been
able
to find anything on the subject in the newsgroups or on the web. If
anyone
could provide any information or examples it would be greatly
appreciated.

Thanks,

Kevin


\

I believe if I include the .h and link the library it would not be truly
dynamic. I would like to load the library using the following sequence at
runtime:

// typedef the instantiation method
typedef Component *(*CreateMethod)(std::string &);

// reference to the base class component
Component *comp;

// Load the component dll for Component1 which is a Component
module = dlopen(library.c_str(), RTLD_NOW);
if (!module)
{
error = dlerror();
throw BepException(“Couldn’t open the library file: %s”, error);
}
else
printf(“Loaded: %s\n”, library.c_str());

// Get the method to create the component
dlerror();
method = (CreateMethod*) dlsym(module, “CreateMethod”);
if ((error = dlerror())) {
throw BepException(“Error Retrieving Create Component Method: %s”,
error);
}

// Create the component and type it to the base class component
comp = (Component *) (*method)(configFile.c_str());

I have found examples on how to do this in Linux but it does not compile
under Neutrino. I have not found anything that leads me to believe that I
can not do this in Neutrino but I am not sure what is missing.

Here is a copy of the makefile (from Jeff Koftinoff’s site
http://www.jdkoftinoff.com/ ) with the -rdynamic for Linux changed
to -Bdynamic for Neutrino. I do not know what the library file should be
changed to (-ldl ?) or if it even exists in Neutrino:

The flags needed to compile all

CXXFLAGS = -fPIC -frtti -O

we need to link in the dll handling library

LDLIBS = -ldl


all : plugtest PlugInFun.so PlugInHappy.so

the main executable has to be linked with the -rdynamic flag

so the plug in libraries can call inherited methods and

access vtables in the main executable.

plugtest : plugtest.o dll.o plugin.o
$(CXX) -Bdynamic $^ -o $@ $(LDLIBS)

link the shared libraries with the -shared flag.

PlugInFun.so : PlugInFun.o
$(CXX) $(CXXFLAGS) -shared $^ -o $@

PlugInHappy.so : PlugInHappy.o
$(CXX) $(CXXFLAGS) -shared $^ -o $@

clean :
-rm *.o

realclean : clean
-rm plugtest *.so


Any suggestions? Have you ever tried this or seen an example in a newsgroup
or on the QNX site?

Thanks for your help,

Kevin









“Markus Loffler” <loffler@ces.clemson.edu> wrote in message
news:9go3pp$7lm$1@inn.qnx.com

First, call your dynamic lib “libcomp.so”
The, just include “comp.h” and link to “libcomp.so”
The latter you do with linker option -Bdynamic -lcomp
Markus



“Kevin Hykin” <> kevin.hykin@bepco.com> > wrote in message
news:9go0ja$5q2$> 1@inn.qnx.com> …
“Markus Loffler” <> loffler@ces.clemson.edu> > wrote in message
news:9gnsaf$3a6$> 1@inn.qnx.com> …
Nothing much to say about it… You can use C++ classes in dynamic
libraries
the same way you use C functions. I do it all the time.
Markus


Thanks, if you could elaborate on how I could do this in the
example
below or refer me to a good example of how to do it, I would really
appreciate it.

From my experience using C and extern “C” dynamic libraries, I
believe
that If I declare a class called:

class Component1 : Component
{
public:
some code here;
private
some code and member variables here;
};

in a file called comp.h and I defined it in a class called comp.cpp and
then
compile to comp.o. I then create a dynamic library using:

qcc -g -shared -W1,-soname,comp.so.0 -o comp.so.0.0 comp.o -lc

and end up with a library file called comp.so. How do you get the
reference
to the class so I can use it? I have tried to put a extern “C” function
that dynamically instantiates the class but Neutrino definitely does not
like that. All I want to do is use a base class reference and return
and
instantiated subclass object to it, or even better, instantiate one in
the
code. Is there something I am missing?

Thanks for your help,

Kevin

“Kevin Hykin” <> kevin.hykin@bepco.com> > wrote in message
news:9gnmh5$t7b$> 1@inn.qnx.com> …
Does anyone know if it is possible to export C++ classes for dynamic
instantiation? I have had no problem creating and using dynamic
libraries
if they have C functions or export “C” in .cpp files. I have not
been
able
to find anything on the subject in the newsgroups or on the web. If
anyone
could provide any information or examples it would be greatly
appreciated.

Thanks,

Kevin




\