Dr-Bracket
Dr-Bracket

Reputation: 5494

How does calling a function from a shared library work?

When you load a shared library right before runtime, and add the symbols (i.e. functions) it provides to a global offset table.

What happens when you call a function it provides? Because you already compiled your code, so the function had to point somewhere already.

Upvotes: 0

Views: 1546

Answers (1)

Akib Azmain Turja
Akib Azmain Turja

Reputation: 1180

Let's assume you have a project hello with the following files:

  • main.cpp
  • function.cpp
  • class.cpp

Now you want to build the project with g++. The build process will go like this:

----------------      -------      --------------        -------------
| function.cpp | ---> | g++ | ---> | function.o |        | libstdc++ |
----------------      -------      --------------        -------------
                                                 \      /
------------          -------      ----------     ------      ---------
| main.cpp | -------> | g++ | ---> | main.o | --> | ld | ---> | hello |
------------          -------      ----------     ------      ---------
                                                 /
-------------         -------      -----------  /
| class.cpp | ------> | g++ | ---> | class.o | /
-------------         -------      -----------

Here g++ is the compiler which compiles your source files into an object file one by one. It just checks for syntax errors, undefined functions or variables. Although cpp (C Preprocessor) is executed before compiler, that's another topic. After all of your source files are compiled, ld , which is the linker, links your object files into one executable. Now, let's say main.cpp calls a function named hello-function , implemented in function.cpp , and uses std::cout, defined libstdc++ (C++ Standard Library). Compiler compiles main.cpp into object file as the code is valid. When all the of object file passed the linker, it will read those objects and finds the functions and variable that are referred. If you don't pass function.o (which contains hello-function implementation) to the linker, you will get an error saying undefined reference . Now, let's assume that main.cpp calls a function which available in an external library. If you don't specify the library to the linker, it will show you undefined reference . When there is no error, linker links all object file into an executable and puts informantion about the shared libraries on which the resulting executable depends.

After your program is compiled and linked, you can execute the program. When you execute it, your operating system (in this case it's Linux) loads the executable into memory. After that, it reads the file and determine which libraries it requires and loads them if they aren't already loaded. Then it executes the program, and whenever it finds a reference to an external library, it translates the reference to actual memory location and calls it. After the program finishs execution, your operating system frees unused memories and unloads any unused libraries.

Also, if your program links with a shared library whose version is different than the version your program linked against during linking stage, you might get a "segmentation fault".

Upvotes: 3

Related Questions