Question

Is there any way to implement linking of object files in C, in such a way that certain objects can only be linked together with each other?

Eg. Trying to link Object 1 and Object 2 will work and produce a working executable, but trying to link Object 1 and Object 3 will either return an error, or the resulting executable will work incorrectly.

Background info: Basically, the situation is Programmer A and Programmer B send their objects to a Linker Company. A and B want to ensure that the Linker Company can only link their objects together, and not link their objects with other Programmers (i.e. Programmer C etc) objects (as it would be illegal).

Jörgen Sigvardsson's answer is useful, but I am wondering if there is a more fool-proof method. Eg. If trying to link illegal objects together using gcc, the Linker Company could determine what symbols are required and just add them to illegal objects, easily defeating the restriction.

Was it helpful?

Solution

Yes, there is a way. In object 1:

extern int object_1_symbol;
int object_2_symbol;

And in object 2:

int object_1_symbol;
extern int object_2_symbol;

This will require you to link these objects together, and prevents you from linking it together with object 3. It isn't fool proof, but it may serve your purposes.

OTHER TIPS

No there is not any way to do what you described.

If you want to make sure that the program either links both foo1.o and foo2.o, or none of them, simply combine them into a single object:

ld -r -o foo-combined.o foo1.o foo2.o

and only give the end-user foo-combined.o

If you want to achieve something else, please restate your question more clearly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top