chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trying to link in modules


From: felix . winkelmann
Subject: Re: Trying to link in modules
Date: Sun, 20 Jun 2021 12:42:27 +0200

> I've got a module mymod in mymod.scm:
>
>[...]
>
>  However, I can't figure out how to do the same thing with mymod as
> non-shared objects.   I tried
>
> csc -c -J mymod.scm
> csc -c mymod.import.scm
> csc -static -o trymod.static mymod.o mymod.import.o trymod.scm
>
> but I got
>
> Undefined symbols for architecture x86_64:
>
>   "_C_mymod_toplevel", referenced from:
>       _f_138 in trymod.o
> ld: symbol(s) not found for architecture x86_64
> clang: error: linker command failed with exit code 1 (use -v to see
> invocation)
> Error: shell command terminated with non-zero exit status 256: 'clang'
> 'mymod.import.o' 'mymod.o' 'trymod.o' -o 'trymod.static' -m64
> -L/usr/local/Cellar/chicken/5.2.0/lib
> /usr/local/Cellar/chicken/5.2.0/lib/libchicken.a -lm

When loading a shared object, the CHICKEN runtime uses the libld API
to obtain the entry point ("C_toplevel") to invoke top-level initialization code
of the module (which also sets up global bindings, etc.). But in a statically
linked executable, all entry points of linked modules must be available under
a separate name, so that they can be distinguished from each other.

What we do is to give the module (the binary *.o module) a name, which is
called "unit", so mymod would have, for example, the unit name "mymod",
which produces the entry point "C_mymod_toplevel". So you can link any
number of separately compiled binary objects (containing modules) and
their names don't clash.

So, to make it work:

    csc -c -J mymod.scm -unit mymod
    # compiling the import library is not needed
    csc -static -o trymod.static -uses mymod mymod.o trymod.scm

The main file (trymod) doesn't need to resolve the imports, so you can omit
linking the import library. "-unit" gives the module a unit name, "-uses" tells
the main module to invoke the toplevel of the linked module.

This mechanism is a source of great confusion, but this mainly comes from
the fact that static linking uses a facility for resolving module toplevel and
initialization code from the whole module and namespacing machinery.

---
BTW, the "csm" utility, provided by the egg of the same name, figures this out
automatically, just put trymod.scm and mymod.scm into an empty directory
and enter

    csm -program trymod

or

    csm -program trymod -static

Add -d and -v to see what the program is doing and how things are compiled.


felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]