Javatpoint Logo
Javatpoint Logo

Static Library Linking in C++

Introduction

A static library, which can be linked into a program at compile time, is a group of object files that have been consolidated into a single file in C++. All of the variables and functions declared in a static library are included in the executable produced when a program is linked to it. I'll go into great depth on how C++'s static library linking works in this response.

Static Library: Creating

A static library must first be built before it can be linked to a program. Each source file we intend to include in the library must first be compiled before we can make a static library from it. The object files are then used to generate an archive file using the "ar command". On Unix-like systems, the archive file's extension is ".a", while on Windows systems, it is .lib.

Consider the scenario where we want to include the two source files foo.cpp and bar.cpp in a static library called libfoobar.a. Each source file would first be compiled into an object file:

The archive file can then be created using the "ar" command:

As a result, the object files foo.o and bar.o are combined into an archive file called libfoobar.a.

Static Library Linking

The location of a static library must be specified to the compiler before we can link a program against it. The -L option, followed by the directory where the library is placed, is used to accomplish this. The name of the library must also be specified using the -l option, but without the lib prefix or the .a extension.

Consider a program called main.cpp that uses libfoobar.a library functions as an example. The program would be assembled and linked as follows:

This instructs the compiler to link the program against the libfoobar.a library and search for it in the current directory (-L. (-lfoobar).

All of the functions and variables declared in the static library are included in the final executable when the program is linked against it. This implies that if the library is huge, the executable's size may rise dramatically.

Symbol References

The linker must resolve any references to symbols declared in the static library when linking a programme against it. This is accomplished by looking for the symbol definition in the object files of the library.

If the symbol has a definition, the linker includes it in the programme and changes the symbol table to refer to the definition if one is found. The linker reports an undefined symbol error if it cannot locate a definition for the symbol.

Assume, for instance, that the main function of our programme calls the function foo that is defined in the library libfoobar.a. The linker looks for the definition of foo in the library's object files when we link the application to it. If it does, it adds the definition to the program and modifies the symbol table to include a reference to the definition. If the definition cannot be located, an undefined symbol error is reported.

Order of Linking (Linking Order)

The linking process might be impacted by the order in which object files and libraries are given on the linker command line. Object files and libraries should typically be specified in ascending sequence of independence.

Consider the case when the programme main.cpp calls into both the libfoo.a and libbar.a libraries. If the symbols described in the libbar.a library are used by the libfoo.a library, we must identify the libraries in the proper order:

Even though main.cpp calls functions from both libraries, the libfoo.a library is mentioned in this example before the libbar.a library. It is necessary to link libbar.a first to ensure that the symbols are defined before they are used because libfoo.a depends on symbols defined in libbar.a.

Optimization of Link-Time

Link-time optimisation is supported by some linkers and compilers (LTO). At the linking stage, LTO does optimisation across object files and libraries, which can lead to better speed and less code.

We must first compile the source files with LTO enabled using the -flto option in order to enable LTO:

With the -flto option, we can then link the program with LTO enabled:

LTO can dramatically boost our code's performance, but it can also make linking take longer and use more memory. Also, not all linkers and compilers support LTO.

Conclusion

We have discussed how C++'s static library linking function in this response. We've seen how to build a static library, link a programme against it, how the linker handles symbol references, and how the arrangement of object files and libraries might impact linking. We have also talked about link-time optimisation, which can make our programmes run faster.


Next TopicConstexpr in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA