473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Dynamic class loading

I'm trying to do the following:

1) The main program consists of
------------ a.h ------------
class A
{
public:
virtual ~A(){}
virtual void b();
};

------------ a.cpp -----------
#include "a.h"

void A::b()
{}

------------ main.cpp ------------
#include <dlfcn.h>
#include <iostream>
using namespace std;

int main()
{
void* handler = dlopen( "../lib/lib.so", RTLD_LAZY );
if( !handler )
{
cout << "Bad" << endl;
cout << dlerror() << endl;
}
}
-----------------------------------

2) Loadable library:

---------------- lib.cpp --------------------
#include "a.h"

class C : public A
{
public:
C() : A(){}
};
extern "C" A* maker()
{
return new C();
}
----------------------------------------------

Compiles ok (gcc 4.1.0, SUSE Linux), but running the program I get:
.../lib/lib.so: undefined symbol: _ZN1A1bEv

What's the matter? nm shows that symbol is defined (in a.o)
Nov 26 '07 #1
2 2018
vl***@mail.ru wrote:
I'm trying to do the following:

1) The main program consists of
------------ a.h ------------
class A
{
public:
virtual ~A(){}
virtual void b();
};

------------ a.cpp -----------
#include "a.h"

void A::b()
{}

------------ main.cpp ------------
#include <dlfcn.h>
#include <iostream>
using namespace std;

int main()
{
void* handler = dlopen( "../lib/lib.so", RTLD_LAZY );
if( !handler )
{
cout << "Bad" << endl;
cout << dlerror() << endl;
Neither 'dlopen' nor 'dlerror' are standard C++ or C functions.
They aren't topical here (if you think they are essential to
your question, which I believe they are).
}
}
-----------------------------------

2) Loadable library:

---------------- lib.cpp --------------------
#include "a.h"

class C : public A
{
public:
C() : A(){}
};
extern "C" A* maker()
{
return new C();
}
----------------------------------------------

Compiles ok (gcc 4.1.0, SUSE Linux), but running the program I get:
../lib/lib.so: undefined symbol: _ZN1A1bEv

What's the matter? nm shows that symbol is defined (in a.o)
Your question has no answer in terms of C++ _langauge_. You need to
ask in the newsgroup where dynamic linking is on topic (most likely
the newsgroup for your OS). Also, since you're using 'gcc' you might
consider posting to 'gnu.g++.help' or 'gnu.gcc.help' since they know
more about how the compiler/linker resolves symbols in such cases.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 26 '07 #2
On Nov 26, 2:18 am, vl...@mail.ru wrote:
I'm trying to do the following:
you may want to make A an abstract class -- there is no need for an
a.cpp:

class A
{
public:
virtual ~A(){}
virtual void b() = 0;
};
a certain compiler version of gcc forbids that the executable exports
symbols.
You need to link your executable with the option -rdynamic to make it
export symbols.
Or you can make certain that the executable does not need to export
any symbols by putting everything required into another shared
library. The advantage being that the same library structure would
also work on Windows where you cannot export symbols from an
executable at all.
Also -- I would wrap dlopen()/dlclose() into a C++ resource wrapper
(
calling dlopen() in the constructor and dlclose() in the destructor --
the constructor should throw something derived from std::exception
which contains all error information -- e.g. what has been returned
from dlerror().
In general you should do this with every C-style resource you
encounter. If there is a do-action and an undo-action wrap it into a C+
+ class which throws on error.
)
Nov 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Parag Joshi | last post by:
hi, I am facing a wierd problem with dyanamic class loading. I have a namespace called ETS.DAO which contains my Data Access Object classes. All these classes implement a common interface which...
17
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. ...
9
by: Ender | last post by:
I have an application that I would like third party developers to be able to create Plug-ins that will be dynamically loaded into our application to extend functionality. I have utilized the...
4
by: Yan Wang | last post by:
Hi!: I encounter this problem when I do some tests: I have one user control with datalist in it. The ID for datalist is "dlTest". Then in this control code behind class, I declare a protected...
1
by: Robert McLay | last post by:
I have been trying to build python on Cray X1. As far as I can tell it does not support dynamic loading. So the question is: How to build 2.4 without dynamic loading? That is: can I build...
0
by: John Allman | last post by:
Hi all, I'm trying to create a setup which allows a program to request an object using strings and get an object of that type. It appears to be mostly working but i have difficulties if i...
13
by: jerryau | last post by:
Hi, I am trying to dynamically create object instances based on a string class name, and then I need to dynamically set values to these objects, but I have no idea how to do this in C#. Here is...
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
11
by: digz | last post by:
Hello, Apologies if this is the wrong group for this question. I want to design an interface , where for a custom functionality , the client writes a new class with the function implementation...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.