Differences between dynamic libraries vs static libraries

Why use libraries?

int main(void)
{
int x = 5;
int y = 8;
int result; result = sum(x, y);
return (0);
}
int sum(int a, int b)
{
return (a + b);
}int main(void)
{
int x = 5;
int y = 8;
int result; result = sum(x, y);
return (0);
}

How they work

How to create them

$ gcc -c sum.c   // produces a sum.o object file
$ ar -rc libforme.a sum.o
$ nm libforme.a// sample outputsum.o:000000000000002e T sum

How to use them

int main(void)
{
int x = 5;
int y = 8;
int result; result = sum(x, y);
return (0);
}
gcc my_program.c// oops... ///tmp/ccGLAk66.o: In function `main':my_program.c:(.text+0x26): undefined reference to `sum'collect2: error: ld returned 1 exit status
gcc my_program.c -L. -lforme -o my_program
  • -L says “look in directory for library files”
  • . (the dot after ‘L’) represents the current working directory
  • -l says “link with this library file”
  • forme is the name of our library. Note that we omitted the “lib” prefix and “.a” extension. The linker attaches these parts back to the name of the library to create a name of a file to look for.
  • -o my_program says “name the executable file my_program”

DIFFERENCES

HOW DOES THE SYSTEM FIND THE LIBRARIES?

GCC

NM

LDD

LDCONFIG

RANLIB

AR

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store