lua-users home
lua-l archive

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


Am 21.11.18 um 12:12 schröbte Luiz Henrique de Figueiredo:
[...] A library is not meant to be totally incorporated into the
program; only modules that provide definitions for the program are
incorporated. Again, this is reasonable and convenient, and works
extremely well in practice. It has been like that for many decades.

The manual for GNU ld seems to support that. Extract from the `-l/--library` option description:

The linker will search an archive only once, at the location where it
is specified on the command line. If the archive defines a symbol
which was undefined in some object which appeared before the archive
on the command line, the linker will include the appropriate file(s)
from the archive.

And a small test:


    $ cat a.c
    #include <stdio.h>

    void a(void) {
      puts("A");
    }


    $ cat b.c
    #include <stdio.h>

    void a(void) {
      puts("A");
    }

    void b(void) {
      puts("B");
    }


    $ cat main.c
    extern void a(void);
    extern void b(void);

    int main(void) {
      a();
      b();
      return 0;
    }


    $ gcc -c a.c

    $ gcc -c b.c

    $ ar r liba.a a.o

    $ ar r libb.a b.o

    $ gcc -o main main.c a.o b.o
    b.o: In function `a':
    b.c:(.text+0x0): multiple definition of `a'
    a.o:a.c:(.text+0x0): first defined here
    collect2: error: ld returned 1 exit status

    $ gcc -o main main.c b.o a.o
    a.o: In function `a':
    a.c:(.text+0x0): multiple definition of `a'
    b.o:b.c:(.text+0x0): first defined here
    collect2: error: ld returned 1 exit status

    $ gcc -o main main.c a.o -L. -lb
    ./libb.a(b.o): In function `a':
    b.c:(.text+0x0): multiple definition of `a'
    a.o:a.c:(.text+0x0): first defined here
    collect2: error: ld returned 1 exit status

    $ gcc -o main main.c b.o -L. -la


So it seems to work in practice on Linux using my current version of the GNU linker if the original object file is in an archive, the modified object file is listed before the archive on the command line, and the modified object file exports at least all the same symbols as the original object file.


Philipp