Static Libraries… What are we talking about??

As soon as we start working with C language, we start compiling our programs in order to make them executable.

One of the main tasks of the Linker (last step of compilation) is to make code of library functions available to your program. The static libraries are the result of the copy of all used functions in your code.

  • First of all we need a .C file where our function is contained.
  • Our second step should be, create a header which contains our function.
  • We must compile our .C file, to make it a .O file
  • At last we need to create the static libary with our function inside it. So… how can we do it??

Using the command ar to create it, with rc option, followed by the name of the library we want to create and finally the name of the functions we want to add to it.

To make it easier if we want to add al the .o files we have in the current directory, our last argument could be *.o.

If we want to compile any functiom that contains another one inside, we could get an error if we dont call the libary that we need to use.

We should add some options to our gcc command to make it work…

  • First of all -L says “look in directory for library files” if we add a dot it means the current directory.
  • -l says “link with this library file followed the name of our library, ommiting the .a prefix of it.
  • And at last -o option followed by teh program we want to compile.

By making all this we are going to be able to compile our programs with our own functions and make the work perfectly.

--

--

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