C++ Shared Library: Dynamic Linking (Eps: 1)
Extend Your Lifetime by Reducing Your Compile Time
Hello-hello, come again to my story, and this story will be different from my previous stories. Finally, I made a story about Software Engineer stuff, whereas previously, I only shared my personal experiences and DevOps stuff. In this story, I will show you how to create and use a shared library, particularly a Dynamic Linking library in C++. I’m not gonna explain to you the shared library and dynamic linking theories because those two things are already well explained on the internet (if you want to know more about those, you can refer to the References section below). I’m going to focus on how to create and use a shared library in technical ways. Without further waiting, let’s dive into it!
Code
We will make a very simple shared library containing a function that will print a text based on the argument passed (we will call this service.cpp). We will call this shared library the main.cpp file. But the difference is, instead of compiling and linking the main.cpp with the service.cpp together, we will compile the service.cpp and the main.cpp separately and then linking them dynamically. This way, if we want to make a change to the main.cpp, we only need to recompile the main.cpp, instead of recompiling both of them.
Let’s start with the service.hpp
Next service.cpp
And finally our main.cpp
Command
When we want to statically link the service.cpp with the main.cpp, we can use this g++ command:
g++ service.cpp main.cpp -o main.o
But because our goal here is to link the library service with our main dynamically, we will use different commands to achieve this. The first one is to compile our service.cpp into an object file:
g++ -c -fPIC service.cpp -o service.o
-c
: tell the compiler to stop after the compilation process and not perform the linking.-fPIC
: from opengenus.org → fPIC option in GCC enables the address of shared libraries to be relative so that the executable is independent of the position of libraries.
Next, from our library object file, we will make a shared object file so that later, we can link it with our main:
g++ -shared -o libservice.so service.o
-shared
: tell the compiler that this will produce a shared library.
Then, we can link our shared library, which is a service, to our main:
g++ main.cpp -L. -lservice -o main.o
-L<path_to_.so_file>
: define the directory where we put our shared object file (.so
). In this case, we put it in the current directory.-l<lib_real_name>
: the name of the library that we want to link. The name used is our.so
file name, but without thelib
prefix (in this case, our.so
file is named libservice.so, so the way we write this flag is-lservice
, remember to leave the lib- prefix and .so suffix).
And finally, we can run our main by including our shared library path:
LD_LIBRARY_PATH=. ./main.o
The environment variable LD_LIBRARY_PATH will tell the dynamic link loaders where to look for the dynamic shared library that is going to be linked.
Thanks for reading my story about dynamic linking in C++. In the next episode, I’ll share more about another type of shared library, which is dynamic loading. See you soon then 👋