WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Expert · question 68 of 100

Describe the differences between static and dynamic libraries in C, and explain how to create and link them.?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C, a library is a collection of pre-compiled object files that contain functions and data that can be used in other programs. Libraries can be classified into two types: static and dynamic.

A static library is a collection of object files that are linked with the program at compile time, and its code is copied into the executable file. This means that the entire library code is compiled into the final program, and the program does not require the library to be present at runtime. Static libraries have the advantage of being faster to load and execute, but they have the disadvantage of increasing the size of the executable.

To create a static library in C, you need to perform the following steps:

Create a set of object files containing the library functions.

Use the ar (archiver) command to create the static library file. The command syntax is as follows:

    ar rcs liblibraryname.a objectfile1.o objectfile2.o ...

This command creates a static library file named liblibraryname.a that contains the object files specified in the command.

To use the static library in your program, you need to link it during the compilation process. You can do this by adding the -l and -L flags to the compiler command, followed by the library name and its location, respectively.

    gcc -o program program.c -L/path/to/library -llibraryname

A dynamic library, on the other hand, is linked with the program at runtime and is loaded into memory by the operating system. This means that the library is not copied into the final executable file, and the program requires the library to be present at runtime. Dynamic libraries have the advantage of reducing the size of the executable, but they have the disadvantage of being slower to load and execute.

To create a dynamic library in C, you need to perform the following steps:

Create a shared object file containing the library functions. The shared object file has the extension .so on Linux/Unix systems and .dll on Windows systems.

    gcc -shared -o liblibraryname.so objectfile1.o objectfile2.o ...

To use the dynamic library in your program, you need to link it during the compilation process. You can do this by adding the -l and -L flags to the compiler command, followed by the library name and its location, respectively.

    gcc -o program program.c -L/path/to/library -llibraryname

In summary, static libraries are linked with the program at compile time and are copied into the executable file, while dynamic libraries are linked at runtime and are loaded into memory by the operating system. Both types of libraries have their advantages and disadvantages, and the choice depends on the requirements of the program.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics