Symbol Resolution

NOTE: C++ and Java allow function overloading through a process called name mangling and the reverse is demangling

How Linker Resolve Duplicate Symbol Names:

/*foo5.c*/
int x = 12345;
int main(){
	f();
	printf("x = 0x%x \n", x);
	return 0;
}
/*bar5.c*/
double x;
void f(){
	x = -0.0;
}
linux> $ gcc -o prog foo5.c bar5.c

Linking with Static Libraries:

/*addvec.c*/

int addcnt = 0;

void addvec(int *x, int *y, int *z, int n){
	 int i;
	 addcnt++;
	 for(i = 0; i < n; i++)
		 z[i] = x[i] + y[i];
}
/*multvec.c*/

int multcnt = 0;

int multvec(int *x, int *y, int *z, int n){
	int i;
	multcnt++;
	for(i = 0; i < n; i++)
		z[i] = x[i] * y[i];
}
linux> gcc -c addvec.c multvec.c
linux> ar rcs libvector.a addvec.o multvec.o
/*main2.c*/
#include <stdio.h>
#include "vector.h"

int x[2] = {1, 2};
int y[2] = {3, 4};

int main(){
	addvec(x, y, z, 2);
	printf("z = [%d %d]\n", z[0], z[1]);
	return 0;
}

-And we can just invoke the library as needed:

linux> gcc -c main2.c
linux> gcc -static -o prog2c main2.o ./libvector.a

-or equivalenty,

linux> gcc -c main2.c
linux> gcc -static -o prog2c main2.o -L -lvector

Pasted image 20260424203256.png
Fig: Linking with static library

How Linkers Use Static Libraries to Resolve References

linux> gcc -static -o prog me.o you.o  -L -lvec
Powered by Forestry.md