Linking Process in C

25 February 2020

Obed N Munoz

Cloud Software Engineer

Static Linking

An example of static linking

// lib.c
int add(int a, int b) {
    return a + b;
}
// static-linking.c
#include <stdio.h>

int add(int, int);
int main() {
  printf("2+3=%d\n", add(2,3));
  return 0;
}
gcc -c static-linking.c -o static-linking.o
gcc -c lib.c -o lib.o
gcc static-linking.o lib.o -o main.o
./main.o
2+3=5
2

Let's analyze the linking symbols

> nm main.o
                 U _add
0000000000000000 T _main
                 U _printf
> nm lib.o
0000000000000000 T _add

Let's see:

What happens?

3

Shared or Dynamic Linking

> ldd `which emacs`

linux-vdso.so.1 (0x00007fff725cf000)
libtiff.so.5 => /usr/lib/libtiff.so.5 (0x00007f46288be000)
libjpeg.so.8 => /usr/lib/libjpeg.so.8 (0x00007f4628829000)
libpng16.so.16 => /usr/lib/libpng16.so.16 (0x00007f46287f1000)
libgif.so.7 => /usr/lib/libgif.so.7 (0x00007f46287e6000)
libXpm.so.4 => /usr/lib/libXpm.so.4 (0x00007f46287d2000)
libgtk-3.so.0 => /usr/lib/libgtk-3.so.0 (0x00007f4628088000)
libgdk-3.so.0 => /usr/lib/libgdk-3.so.0 (0x00007f4627f8b000)
libpango-1.0.so.0 => /usr/lib/libpango-1.0.so.0 (0x00007f4627f41000)
libcairo.so.2 => /usr/lib/libcairo.so.2 (0x00007f4627e1a000)
.
.
.
4

C Standard Library

5

Let's code: Strings Manipulation library

classify.obedmr.com/get-lab/ap-labs/str-lib

6

Resources and Credits

This material is genereated thanks to some extracts from following resources:

7

Thank you

Obed N Munoz

Cloud Software Engineer

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)