Linking in C

Add external functionality

Let's start with a simple one


int add(int a, int b) {
    return a + b;


int add(int, int);
int main() {
  printf("2+3=%d\n", add(2,3));
  return 0;

Ok, but how to link it?

gcc -c static-linking.c -o static-linking.o
gcc -c lib.c -o lib.o
gcc static-linking.o lib.o -o linked

It Works!


Let's analyze the linking symbols

nm linked

                  U _add
0000000000000000 T _main
                  U _printf

nm lib.o

0000000000000000 T _add

Compile, link and run static-linking2.c, what happened?

Shared or Dynamic Linking

ldd $(which emacs) (0x00007fff725cf000) => /usr/lib/ (0x00007f46288be000) => /usr/lib/ (0x00007f4628829000) => /usr/lib/ (0x00007f46287f1000) => /usr/lib/ (0x00007f46287e6000) => /usr/lib/ (0x00007f46287d2000)

Take a look on ldconfig tool and common shared library

C Standard Library

  • Input and output: <stdio.h>
  • Character Class Tests: <ctype.h>
  • String Functions: <string.h>
  • Mathematical Functions: <math.h>
  • Utility functions: <stdlib.h>
  • Diagnostics: <assert.h>
  • Variable argument lists: <stdarg.h>
  • Non-local jumps: <setjmp.h>
  • Signals: <signal.h>
  • Date and Time functions: <time.h>
  • Implementation-defined limits: <limits.h> and <float.h>

Let's code: Strings Manipulation library

  • Follow instructions from:

Resources and Credits

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


  • Obed N Muñoz Reynoso
    • Cloud Software Engineer
    • obed.n.munoz@gmail | tec |