Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamical loading of C modules

0 views
Skip to first unread message

Stefan Wils

unread,
May 10, 1998, 3:00:00 AM5/10/98
to

Hi

I'm currently designing an interpreted language, just for the fun. But
when it comes to programs that need speed, I want the programmer to be
able to write some parts in C(++), compile it and then, from his
program that is being interpreted, load the piece of compiled C code
dynamically.

This way the syntax of 'my' language is extended with some keywords,
from which the corresponding code is in the loaded module.

Is this possible in C(++)/Unix ? If it is, could anybody please put me
on the road on how to implement such a system ? I could use some
extra table file to declare the added syntax of the loaded 'module',
but that still doesn't solve the problem of dynamic loading and
calling procedures in the new piece of code. And what about parameter
passing ? I'm not a C and Unix guru, so...go easy on me :)

Stefan Wils
stefa...@zorro.ruca.ua.ac.be

For every question you ask, answer two.

jer...@nl.euro.net

unread,
May 10, 1998, 3:00:00 AM5/10/98
to

Stefan Wils <stefa...@zorro.ruca.ua.ac.be> wrote:
: This way the syntax of 'my' language is extended with some keywords,

: from which the corresponding code is in the loaded module.

: Is this possible in C(++)/Unix ? If it is, could anybody please put me
: on the road on how to implement such a system ? I could use some
: extra table file to declare the added syntax of the loaded 'module',
: but that still doesn't solve the problem of dynamic loading and
: calling procedures in the new piece of code. And what about parameter
: passing ? I'm not a C and Unix guru, so...go easy on me :)

This may not solve all your problems, but here's how I played around
with dynamic linking a while ago. First, write a module, which we'll
built into a dynamically linkable lib later on. Let's call it foo.c:

#include <stdio.h>

void do_it()
{
fprintf(stderr, "Hello world!\n");
}

That was pretty easy, huh?

Compile it into an object-file:

gcc -o foo.o foo.c

and create a shareable lib:

ld -o foo.mod -Bshareable foo.o

We now have a module called 'foo.mod'.

Next write the program which is going to dynamically load the lib (according
to stringent naming conventions, we'll call it bar.c :-) ):

#include <stdio.h>
#include <dlfcn.h>

int main()
{
void *handle;
void *fn;
void (*func)();

if(!(handle=dlopen("foo.mod", 1))) /* This loads the module */
{
fprintf(stderr, "dlopen returned NULL\n");
exit(1);
}

if(!(fn=dlsym(handle, "_do_it"))) /* This retrieves the address
of the function, do not forget
the leading underscore */
{
fprintf(stderr, "dlsym returned NULL\n");
exit(1);
}

func=fn;
(*func)(); /* This executes the function */

dlclose(handle);
}

The man-page to read here is dlopen.

In this example, you know what the function do_it() looks like - the
problem you're facing is a bit more difficult. What I would do is:

* Require that all 'plugins' are located in one directory
* Require that all plugins have a function called 'describe' or something,
of which the prototype is known. This function should fill
a struct which describes:
* The name of the keyword in your language
* The name of the symbol in the module
* Number and types of arguments the function takes
* Type of return-value
* On startup, walk through all the files in the directory, retrieve
the address of the describe-function of each of 'em, call that
function, and store the resulting struct in an array.
* That should be enough to recognize the keywords in your language,
and call the appropriate function, with the appropriate arguments.

Let us know if it really works!

later,

--
Jeroen Koops * * EuroNet Internet BV
Special Projects Team * * Herengracht 208 - 214
* 1016 BS Amsterdam
E-mail: jer...@nl.euro.net * Tel: +31 20 535 55 55

Samphlett

unread,
May 10, 1998, 3:00:00 AM5/10/98
to

stefa...@zorro.ruca.ua.ac.be (Stefan Wils) wrote:

>I'm currently designing an interpreted language, just for the fun. But
>when it comes to programs that need speed, I want the programmer to be
>able to write some parts in C(++), compile it and then, from his
>program that is being interpreted, load the piece of compiled C code
>dynamically.
>

>This way the syntax of 'my' language is extended with some keywords,
>from which the corresponding code is in the loaded module.
>
>Is this possible in C(++)/Unix ? If it is, could anybody please put me
>on the road on how to implement such a system ? I could use some
>extra table file to declare the added syntax of the loaded 'module',
>but that still doesn't solve the problem of dynamic loading and
>calling procedures in the new piece of code. And what about parameter
>passing ? I'm not a C and Unix guru, so...go easy on me :)

Take a look at the way MATLAB does it! They've been doing it for years, with
really good results.

Steve

har...@foo.bar

unread,
May 11, 1998, 3:00:00 AM5/11/98
to

Er,
You mean like a shell?

Stefan Wils (stefa...@zorro.ruca.ua.ac.be) wrote:
: Hi

: I'm currently designing an interpreted language, just for the fun. But


: when it comes to programs that need speed, I want the programmer to be
: able to write some parts in C(++), compile it and then, from his
: program that is being interpreted, load the piece of compiled C code
: dynamically.

: This way the syntax of 'my' language is extended with some keywords,
: from which the corresponding code is in the loaded module.

: Is this possible in C(++)/Unix ? If it is, could anybody please put me
: on the road on how to implement such a system ? I could use some
: extra table file to declare the added syntax of the loaded 'module',
: but that still doesn't solve the problem of dynamic loading and
: calling procedures in the new piece of code. And what about parameter
: passing ? I'm not a C and Unix guru, so...go easy on me :)

: Stefan Wils

Nicolas Thery

unread,
May 11, 1998, 3:00:00 AM5/11/98
to

Stefan Wils wrote:
>
> Hi
>
> I'm currently designing an interpreted language, just for the fun. But
> when it comes to programs that need speed, I want the programmer to be
> able to write some parts in C(++), compile it and then, from his
> program that is being interpreted, load the piece of compiled C code
> dynamically.
>
> This way the syntax of 'my' language is extended with some keywords,
> from which the corresponding code is in the loaded module.
>
> Is this possible in C(++)/Unix ? If it is, could anybody please put me
> on the road on how to implement such a system ? I could use some
> extra table file to declare the added syntax of the loaded 'module',
> but that still doesn't solve the problem of dynamic loading and
> calling procedures in the new piece of code. And what about parameter
> passing ? I'm not a C and Unix guru, so...go easy on me :)

I think there are no generic way to write a shared library for every
unixes. For linux, you can look at the GCC HOWTO and the the ELF HOWTO.

Both howtos explain how to write a shared lib.


--
Nicolas Thery mailto: nicola...@wanadoo.fr
"The number of UNIX installations has grown to 10, with more expected."
(The UNIX Programmer's Manual, 2nd Edition, June 1972)

James Youngman

unread,
May 11, 1998, 3:00:00 AM5/11/98
to

>>>>> "nt" == Nicolas Thery <nicola...@wanadoo.fr> writes:

nt> I think there are no generic way to write a shared library for every
nt> unixes.

Well, there is, practically speaking. You can use GNU libtool and
let it do it for you. It copes with all the odd differences between
systems for you.

0 new messages