Is it possible to link in multiple steps / is linking "associative"?

c linker

48 просмотра

2 ответа

3462 Репутация автора

I'm trying to figure out whether it's possible to link object files together in multiple steps, provided that the "order" is preserved. (Alternatively, whether linking* is "associative" or not).

*I'm vaguely aware that there are some subtleties to linking like linker scripts, weak symbols &c. I'm mostly curious about what happens in the simplest case for an "ordinary" C project where every symbol is unique.

I have a trivial project with the following structure:

├── child.c
├── child.h
├── grandparent.c
├── grandparent.h
├── main.c
├── parent.c
└── parent.h

It's built using the following script:


# build objects
gcc -c -o grandparent.o grandparent.c
gcc -c -o parent.o parent.c
gcc -c -o child.o child.c
gcc -c -o main.o main.c

# link all at once
gcc -o main main.o grandparent.o parent.o child.o

Here's child.h

int c_add();

And child.c

#include "child.h"
int c_add(int x, int y) { return x + y; }

Here's parent.c

#include "parent.h"
int p_add(int x, int y) { return c_add(x, y); }

and grandparent.c

#include "grandparent.h"
int gp_add(int x, int y) { return p_add(x, y); }

(parent.h and grandparent.h are trivial variations of child.h)

I'm trying to figure out whether it's possible to link the four object files of the project together in three steps, like in the following (strawman syntax):

$ combine-objfile -o bar.o parent.o child.o
$ combine-objfile -o foo.o main.o grandparent.o
$ gcc -o main foo.o bar.o

Where combine-objfile is a hypothetical utility that combines two files of whatever-format-a-.o-is into another file of the same format, satisfying the same invariants (and allowing references to not-yet-defined symbols)

In case the answer is platform-specific, I'm most curious about x86 Linux.

Автор: Gregory Nisbet Источник Размещён: 08.11.2017 10:31

Ответы (2)

1 плюс

96969 Репутация автора

"Linking" is the process of constructing an executable from one or more object files. It is possible in principle to combine multiple object files in a way that leaves some symbols unresolved, but, to a first approximation, the result is not an executable, and therefore what you have done in that case is not linking.

In practice, one does not generally combine multiple object files into a single, monolithic, non-executable object file. Instead, one builds libraries. It is indeed possible to add objects to a library incrementally, but build systems are not usually structured to do that, because it's not very useful. Instead, if one wants a library, one generally builds all the objects that will go into it first, and then forms the library from them. Occasionally, that involves adding the contents of one library to another, but rarely does it involve adding objects to an existing library.

Автор: John Bollinger Размещён: 08.11.2017 10:45

1 плюс

93565 Репутация автора


Yes, it is possible. It may not be associative in the mathematical sense, but you likely do not really need that.

The macOS and Linux link command (ld) has a switch, -r, that says to link into a new object file instead of an executable.

Автор: Eric Postpischil Размещён: 08.11.2017 11:04
Вопросы из категории :