Dynamic loading problem: My app can load fancy shared objects writtenin C, but it fails if they were

R

Ramon F Herrera

I have an app which dynamically loads lots of plugins (*.so files)
which contain Oracle stuff and are written in C. Being a recent C++
convert (huge fan!), I decided to add some modules written in C++.
Much to my surprise the dynamic loading fails, even for the simplest
of modules.

This is the relevant code:

handle = dlopen((mod_dir + "/" + filename).c_str(), RTLD_NOW); // This
succeeds

function = dlsym(handle, "function_name"); // This fails

Fortunately, there is a dlerror() function which allows me to diagnose
the 1st. statement above, but I don't know how to diagnose problems
with the 2nd. statement.

TIA,

-Ramon

ps: this is the C++ module that I have failed to load:

bool
abc(int)
{
int i = 123;

return true;
}
 
R

Ramon F Herrera

I have an app which dynamically loads lots of plugins (*.so files)
which contain Oracle stuff and are written in C. Being a recent C++
convert (huge fan!), I decided to add some modules written in C++.
Much to my surprise the dynamic loading fails, even for the simplest
of modules.

This is the relevant code:

handle = dlopen((mod_dir + "/" + filename).c_str(), RTLD_NOW); // This
succeeds

function = dlsym(handle, "function_name");  // This fails

Fortunately, there is a dlerror() function which allows me to diagnose
the 1st. statement above, but I don't know how to diagnose problems
with the 2nd. statement.

TIA,

-Ramon

ps: this is the C++ module that I have failed to load:

bool
abc(int)
{
    int i = 123;

    return true;

}


In case, it matters, this is the way my *.so files are built:

g++ -shared -fpic -L/usr/local/lib -lboost_regex -lboost_date_time

I have tried removing the Boost dependencies.

-Ramon
 
R

Ramon F Herrera

Ramon F Herrera <[email protected]> kirjutas:







In order to support function overloading, C++ uses name mangling, meaning
that the name of the function in the object file is not what you expect.
You probably want to use

extern "C" bool abc(int);

The problem and the solution are platform-independent, but the exact
mechanisms (dlopen, dlsym) are platform-specific. On a platform where you
have dlopen/dlsym, you also probably have the nm utility for listing the
names in the object files.

hth
Paavo

Hi Paavo,

My platform is Linux, and 'nm' indicates that the function is indeed
present.

I cannot use:

extern "C" bool abc(int);

Because there will be hundreds of functions whose names are not know
at compile time. I used to have such statement in the past, before I
decided to convert to whole program to dynamic (on-demand) loading.

Thx,

-Ramon
 
R

Ramon F Herrera

Ramon F Herrera <[email protected]> kirjutas:






For your example (bool abc(int)) I get  

paavo@polaris:~/tmp> nm test4.o
00000000 T _Z3abci

This name "_Z3abci" is the one dlsym() could find. If you think it will
be easier and more maintainable to use such names than to add extern "C"
to your function declarations, go ahead (note that the name mangling is
implementation-defined, so it can change even in the next version of
g++).




In a plugin-style architecture one does not need more than one
dynamically resolved name per plugin, in principle. One declares an
extern "C" registration function, which is looked up by dlsym() (or
equivalent), then called. The registration function performs registration
of any other needed "exported" functions by standard C++ techniques,
passing normal function pointers.


> I'm starting to become curious why you switched over
> to dynamic loading in the first place? This is a lot
> of extra maintenance, which should be
> considered only if absolutely necessary.

It is definitely necessary. The base/core program itself is very
simple, and its only job is to identify the plugin required to prepare
a sql query and execute it. There are hundreds of different plugins,
each one specialized in one particular case. The server never ends
while new plugins are developed and the existent ones modified.

I never intended my first (non-dynamic loading) version to make it
into production. I just started that way to avoid these dynamic
loading complications.

-Ramon
 
I

Ian Collins

Ramon said:
I have an app which dynamically loads lots of plugins (*.so files)
which contain Oracle stuff and are written in C. Being a recent C++
convert (huge fan!), I decided to add some modules written in C++.
Much to my surprise the dynamic loading fails, even for the simplest
of modules.

This is the relevant code:

handle = dlopen((mod_dir + "/" + filename).c_str(), RTLD_NOW); // This
succeeds

function = dlsym(handle, "function_name"); // This fails

Fortunately, there is a dlerror() function which allows me to diagnose
the 1st. statement above, but I don't know how to diagnose problems
with the 2nd. statement.

http://en.wikipedia.org/wiki/Name_mangling

Declare your functions extern "C".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top