Skip to content

C++ compiling

Published:

C++ is a compiled language, which means that the code you write is translated into machine code before it is executed. While compiling a small program is usually quite painless, things can get complicated when the project grows.

Static and dynamic libraries

When you compile a program, you can link it to libraries. There are two types of libraries: static and dynamic.

Static libraries

A static library is a file that contains compiled code that can be linked to a program at compile time. The code from the library is copied into the program, and the program is then compiled into a single executable file.

To create a static library, you can use the ar command to create an archive of object files.

# Create a static library
ar rcs libmylib.a file1.o file2.o

To check what libraries are linked to a binary, you can use the ldd command.

Find what libraries are linked statically to a binary

nm -D binary

Dynamic libraries

A dynamic library is a file that contains compiled code that can be linked to a program at run time. The code from the library is not copied into the program; instead, the program is linked to the library at run time.

To create a dynamic library, you can use the gcc command with the -shared option.

# Create a dynamic library
gcc -shared -o libmylib.so file1.o file2.o

To check what libraries are linked to a binary, you can use the ldd command.

# Check what libraries are linked to a binary
ldd binary

Data in Elf files

RPATH

rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries.

# Get rpah of a library
readelf -d bazel-bin/dlinear/libdlinear.so | grep 'R.*PATH'

SONAME

soname is the name of the shared library. It usually considered by other binaries to link against the right version of the shared library at run time.

# Get the SONAME
objdump -p libdlinear.so | grep SONAME

NEEDED

When a binary is linked to any number of shared libraries, they are marked as NEEDED in the binary to ensure they will be loaded at runtime. The NEEDED field is usually taken from the SONAME of the shared library. If the SONAME is not present, the NEEDED field will be the path to the file.

# Get the NEEEDED
objdump -p libdlinear.so | grep NEEDED