OT: dynamically loading functions

Hi.
I’m doing a pluggable AI for my game, so I need to allocate and execute
functions from dynamically loaded libraries.
I know how to do that in Win32, and have the code running so far.
The problem is how to do it in Linux. I have to use dlopen() to open the
library and dlsym() to get the symbol,
but how do I know the symbol’s name? What kind of a name is the one for
Windows anyway? I found it out with depview from the Platform SDK, but I’d
rather have my function exported as “GetInformation”, since it’s the only
one like that in the dll (not overloaded or anything).
Are there conventions on how to call export functions?
How do I link a dll in Linux, anyway? I tried -shared, but that got me a
program which segfaulted.

Thanks in advance…

Pius II.

my code snippet:
#if defined(WIN32)
libraryHandle=LoadLibrary(library);
if (libraryHandle==NULL)
CON_ConOut(“Library %s not found.”,library);

GetInformation=(TGetInformation)GetProcAddress(libraryHandle,"?GetInformatio
n@@YAXPADHPAH at Z");
#elif defined(LINUX)
libraryHandle=dlopen(library,0);
GetInformation=(TGetInformation)dlsym(libraryHandle,“How’sItCalled?”);
#endif
.
.
.
#ifdef LINUX
dlclose(libraryHandle)
#endif

i wrote a little library for cross-platform loading of dynamic libraries, also
includes a nice helper function for finding and loading plugins, if you want
ill email it to you (its not very big but i dont want to stick the atachment p
on the list, ill most likely have it up on a webserver within the next few
days…)

Jess

“Pius II.” wrote:

Hi.
I’m doing a pluggable AI for my game, so I need to allocate and execute
functions from dynamically loaded libraries.
I know how to do that in Win32, and have the code running so far.
The problem is how to do it in Linux. I have to use dlopen() to open the
library and dlsym() to get the symbol,
but how do I know the symbol’s name? What kind of a name is the one for
Windows anyway? I found it out with depview from the Platform SDK, but I’d
rather have my function exported as “GetInformation”, since it’s the only
one like that in the dll (not overloaded or anything).
Are there conventions on how to call export functions?
How do I link a dll in Linux, anyway? I tried -shared, but that got me a
program which segfaulted.

For libksd (http://libksd.sourceforge.net/) I wrote some dynamic library
loading code that is written/tested for windows and Linux, written (not
test) for BeOS, and started to write for (but gave up on) MacOS. There
is also a VERY complex (or overly featureful, if that sounds friendlier
:slight_smile: plugin system that is pretty simple to use (given that you’re using
C++ since it uses a lot of templates). If you download libksd the
relavent files are: SharedLibrary.cpp and SharedLibrary.h

Under Linux you should compile the source files for your library with
"-fPIC" and link them with “-shared”. THe program that dynamically
loads the library should be linked with “-rdynamic”. I hope this
helps!!

-- David Snopek

/-- libksd –
| The C++ Cross-Platform Game Framework
| Only want to write it once??
| http://libksd.sourceforge.net
------------

Hi.
I’m doing a pluggable AI for my game, so I need to allocate and execute
functions from dynamically loaded libraries.
I know how to do that in Win32, and have the code running so far.
The problem is how to do it in Linux. I have to use dlopen() to open the
library and dlsym() to get the symbol,
but how do I know the symbol’s name? What kind of a name is the one for
Windows anyway? I found it out with depview from the Platform SDK, but I’d
rather have my function exported as “GetInformation”, since it’s the only
one like that in the dll (not overloaded or anything).
Are there conventions on how to call export functions?
How do I link a dll in Linux, anyway? I tried -shared, but that got me a
program which segfaulted.

You need to add -fPIC -DPIC to all of your compile flags before you make a
shared library. This ensures that all of your code and the definitions in
the include files produce position-independent code, which is necessary
for shared libs.On Mon, 19 Feb 2001, Pius II. wrote:


Rafael R. Sevilla <@Rafael_R_Sevilla> +63 (2) 4342217
UP Diliman Mobile Robotics Laboratory +63 (917) 4458925
OpenPGP Key ID: 0x0E8CE481

You need to add -fPIC -DPIC to all of your compile flags before you make a
shared library. This ensures that all of your code and the definitions in
the include files produce position-independent code, which is necessary
for shared libs.

PIC is usually necessary for code that has to be shared but not a
requirement for dynamically linked code. non-PIC code will be relocated at
load time and thus won’t be shared with other processes using the same
code, but if you (typically) only have one instance running at a given
time then it’s no loss at all

note that PIC often imposes a small penalty since the compiler usually
reserves a register as a base pointer, which has to be initialized in a
PC-relative way. this is mostly a problem for register-starved
architectures (x86). architectures having pc-relative addressing have no
problem but this is rare, only I can think of is AMD’s forthcoming x86-64

of course, OSes that relocate shared objects to a common address (like
Windows) don’t have this problem at all

--------------C9DBFDA1B4370D90D3688723
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Jess,

i wrote a little library for cross-platform loading of dynamic libraries, also
includes a nice helper function for finding and loading plugins, if you want
ill email it to you (its not very big but i dont want to stick the atachment p
on the list, ill most likely have it up on a webserver within the next few
days…)

I’d be interested in the code … actually, I started on something like this as
well but never finished it.

And - Sam - definitely add this to the SDL 1.3 wishlist.

Ciao
Andreas–
| Andreas Schiffler aschiffler at home.com |
| Senior Systems Engineer - Deskplayer Inc., Buffalo |
| 4707 Eastwood Cres., Niagara Falls, Ont L2E 1B4, Canada |
| +1-905-371-3652 (private) - +1-905-371-8834 (work/fax) |

--------------C9DBFDA1B4370D90D3688723
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public “-//w3c//dtd html 4.0 transitional//en”>

Jess,
i wrote a little library for cross-platform loading of dynamic libraries, also
includes a nice helper function for finding and loading plugins, if you want
ill email it to you (its not very big but i dont want to stick the atachment p
on the list, ill most likely have it up on a webserver within the next few
days....)
 
I'd be interested in the code ...  actually, I started on something like this as well but never finished it.

And - Sam - definitely add this to the SDL 1.3 wishlist.

Ciao
Andreas

-- 
|  Andreas Schiffler                    aschiffler at home.com  |
|  Senior Systems Engineer    -    Deskplayer Inc., Buffalo  |
|  4707 Eastwood Cres., Niagara Falls, Ont  L2E 1B4, Canada  |
|  +1-905-371-3652 (private)  -  +1-905-371-8834 (work/fax)  |
 

--------------C9DBFDA1B4370D90D3688723–

Thanks to you all. I was able to build a windows library without cryptic
function names using a .def file and to build a Linux shared library. But
one problem remains: the Linux exports are still completely messed up
(GetInformationTsatZPlayerSpriteZCCAbd or something the like).
Is there a mechanism like the .def file, or a parameter to have gcc build
human-readable names?
I found --demangle, but this seems just to demangle the names in the
information printed to the screen, not in the output files.

Pius II.

“Pius II.” wrote:

Thanks to you all. I was able to build a windows library without cryptic
function names using a .def file and to build a Linux shared library. But
one problem remains: the Linux exports are still completely messed up
(GetInformationTsatZPlayerSpriteZCCAbd or something the like).
Is there a mechanism like the .def file, or a parameter to have gcc build
human-readable names?
I found --demangle, but this seems just to demangle the names in the
information printed to the screen, not in the output files.

Pius II.

Are you using C or C++? If c++ you need to add ‘extern “C”’ to your
function delclarations…

-- David Snopek

/-- libksd –
| The C++ Cross-Platform Game Framework
| Only want to write it once??
| http://libksd.sourceforge.net
------------

Well here’s my little library for cross-platform dynamic library
loading/handling.
you can get it from either of the following locations:
http://www.lbjhs.net/~jessh/libjdll.tar.gz
http://www.lbjhs.net/~jessh/libjdll.zip

it is only heavily tested on Linux but includes support for many other
OS’s. If you find any bugs or add anything please send me a patch or
atleast let me know. it also includes a function(jdll_ScanDir) to assist
in the loading of plugins, this function needs some work to make it more
portable, so if you want to use it on a non-unix os please feel free to
fix it and send me a patch, or you can wait for me to fix it.

Jess Haas
@Jess