Static and Dynamic Libraries 

One of the task of the compiler is to invoke the linker to link the libraries to add the code to the main executable program. There are 2 types of libraries.

Statically linked libraries vs Dynamically linked libraries.

Statically linked libraries can be created as .a(linux) and (.lib) windows. The code and data will be statically shared to the application.

In case of dynamically linked libraries , the object file created will have .so(linux) and .dll(windows) extension.

These are the parameters you can consider to chose whether you want statically linked library or dynamically linked library.

  1. Size – Dynamically linked library will share the code. Hence collectively the size of all the executable using the library will be less compared to statically linked.
  2. Memory foot print – Same copy of code can be resident in the memory at the same time when the applications using the same static library is running. This can be optimized if you use dynamically linked library.
  3. Performance and loading time – Statically linked libraries will take less time to load compared to dynamically linked library since it doesn’t need to load the library at the runtime or load time and resolve the symbols.
  4. Version dependency – Since the code is compiled into the binary there won’t be any run time version dependency for the applications that statically link libraries.
  5. Recompilation requirement – DLL doesn’t need any recompilation for the applications if the re is a bug fix. Only the DLL needs to be recompiled.

Note: Any time you use to implement DLL suggest to use some capability or versions, so that your application can avoid run time symbol check to verify the API or feature is supported or not.

Source: Static and Dynamic Libraries | Set 1 – GeeksforGeeks

call_lib.h

int add(int a, int b);

call_lib.c
#include "call_lib.h"

int add(int a, int b)
{
	int ret = 0;

	ret = a + b;

	return ret;
}

application.c

#include <stdio.h>
#include "call_lib.h"

#define FIRST_NUM 2
#define SECOND_NUM 3

int main(int argc, char *argv[])
{
	printf("output is %d\n", add(FIRST_NUM , SECOND_NUM));

	return 0;
}

to build the library
ar rcs libcall_lib.a call_lib.o

To build the final library
gcc -o sample -L . -I . application.c -lcall_lib

About the Author

Yogi

21 years of experience in various layers of software. Primarily experienced in system side software design and development on server management software. Interested in linux development in x86 and arm architecture.