In this article, we’re going to talk about linking shared objects in Linux using the `gcc` compiler and some other tools. Shared objects are like libraries that can be linked into your program at runtime, which means they don’t have to take up extra space on disk or memory when you run your program.
To create a shared object from your compiled code (which is stored in .o files), you first need to compile those object files into a static library using the `ar` tool. This creates an archive file that contains all of your compiled code in one convenient package. Here’s what it might look like:
# This script uses the `ar` tool to create a static library from compiled object files.
# The library will be named `libmylib.a` and will contain the compiled code from `myobj1.o` and `myobj2.o`.
# The `ar` tool is used to create an archive file.
# The `r` flag indicates that the archive file will be created or updated.
# The `c` flag indicates that a new archive file will be created if it does not already exist.
# The `s` flag creates an index for the archive file, making it easier to access specific files within the archive.
# The name of the archive file is specified after the `rcs` flags.
# The names of the object files to be included in the archive are listed after the archive file name.
ar rcs libmylib.a myobj1.o myobj2.o
This command tells `ar` to create a new library called “libmylib.a” and add the object files “myobj1.o” and “myobj2.o” to it. The ‘r’ option stands for “replace”, which means that if there are any existing objects with those names in the archive, they will be replaced by the new ones.
Now that you have your library, you can link it into a shared object using `gcc` again but this time, with some extra flags to tell it what’s going on:
# This script links a library into a shared object using gcc with some extra flags to specify the library and its location.
# Compile the source code "myprog.c" and output the executable "myprog"
gcc myprog.c -o myprog
# Use the "-shared" flag to create a shared object instead of an executable
# Use the "-fPIC" flag to generate position-independent code, which is required for shared objects
# Use the "-L" flag to specify the location of the library archive "mylib.a"
# Use the "-l" flag to specify the name of the library to be linked
gcc -shared -fPIC -L/path/to/mylib.a -lmylib -o myprog.so
Let me break this down for you:
– `myprog.c` is the source code file that needs to use your library functions.
– The `-o myprog` flag tells `gcc` what to name the output executable (in this case, “myprog”).
– The `-shared` flag tells `gcc` to create a shared object instead of a regular program.
– The `-fPIC` flag tells `gcc` to generate position-independent code, which is necessary for creating shared objects that can be loaded into memory at runtime.
– The `-L/path/to/mylib.a` flag tells `gcc` where to find your library archive file (in this case, “mylib.a”).
– Finally, the `-lmylib` flag tells `gcc` to link in your library at runtime. Note that you don’t need to specify the full path for the library here just its name without any extensions or directories.
And there you have it! Your shared object is now ready to be used by other programs, and can even be loaded into memory multiple times if needed (since it’s a shared resource). Pretty cool, huh?