473,803 Members | 3,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2042
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
1411
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 is also in the same namespace. I have a namespace ETS.Controller which contain my comtroller classes. My Web client instantiates the controller who in turn fires appropriate methods on the DAOs. In the controller i am using dynamic class loading...
17
4814
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. The trick is, the instatiation will be based on a string with the exact same name as the type that I am trying to instantiate. Obviously, writing a fifty element long switch statement is an inferior solution. So, how can I instantiate based on...
9
4496
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 "Let Users Add Functionality to Your .NET Applications with Macros and Plug-Ins" article at MSDN for the dynamic loading of DLLs http://msdn.microsoft.com/msdnmag/issues/03/10/Plug-Ins/default.aspx
4
2616
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 attribute like "DataList dlTest" Then I create a test asp page to load my control dynamically. by using "LoadControl("controls\MyTest.ascx")" The load is fine and work perfectly. But dlTest in the Page_Load method of my control class is always...
1
1812
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 2.4 where all the extensions are archived in libpython2.4.a as a static library? Building on the Cray X1 is slow, so I have been trying to also build it under Linux without dynamic loading since it
0
1893
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 attempt to load a module at runtime and then request an object of that type. Essentially, my code works as follows: I have a registry class which has a method to register a class. It
13
1864
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 the example of what I need to do: ******Dog.cs****** namespace MySystem {
9
2987
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 got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
11
3111
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 and then puts the name of the Class as a configuration parameter or something and then the main program loads the new class like a module and the client can now write code using the new Functions. I suppose this problem has been solved before , if...
5
2589
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 algorithm to solve a certain messy problem, and many different things. For that I often use several general modules that I have written, like implementation of certain data structures, and small general "utility" functions/classes, plus of course...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.