The compiler only ensures that all the required symbols are defined, but actual loading happens when the executable runs. This is because shared libraries are not compiled into the executable, but rather are dynamically loaded at runtime.
This means that both the compiler and the resulting executable need to be able to locate the library. Shared libraries are a little different – they’re already binaries in the ELF format, rather than just a bag of objects. We just need to make sure that the compiler is able to find it and then everything will be compiled and linked into the final executable.
Static libraries are quite straightforward. Let’s take a step back and discuss what it takes to use a shared library. I already pointed out an important clue along the way – the runtime path of the shared library. We could conclude that an issue lies with the installation process itself then. Why is that? Also, we were able to run the tests – also an executable – just fine. test_project/build/MathsDemo: error while loading shared libraries: libAdd.so: cannot open shared object file: No such file or directoryĮverything works fine up to the point where we try to actually run the executable. Let’s check the final piece of the puzzle – building and running an executable with the installed library: $ cmake -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_PREFIX_PATH:PATH=(pwd)/install -B test_project/build -S test_project How about running tests? $ cd cmake_fundamentals/build Some new output about the runtime path shows up, but the project builds and installs just fine.
Installing: /home/user/install/lib/cmake/Maths/Maths-release.cmake Installing: /home/user/install/lib/ libMaths.so - Set runtime path of "/home/user/install/lib/libMaths.so" to "" Installing: /home/user/install/lib/ libSubtract.so Installing: /home/user/install/lib/ libAdd.so $ cmake -DCMAKE_BUILD_TYPE:STRING=Release -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/install -B cmake_fundamentals/build -S cmake_fundamentals Let’s now see what happens if we try building a shared library instead: $ rm -rf cmake_fundamentals/build/ test_project/build/ install/ Right, everything seems to work just fine – the tests pass, the installed library can be successfully linked into an executable. The test_project needs to be provided with context necessary to find the installed Maths library, we do so explicity by supplying the CMAKE_PREFIX_PATH variable with a path to the install directory: $ cmake -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_PREFIX_PATH:PATH=(pwd)/install -B test_project/build -S test_project Passed 0.00 secġ00% tests passed, 0 tests failed out of 2 Passed 0.00 secĢ/2 Test #2: maths.subTest. Test project /home/user/cmake_fundamentals/buildġ/2 Test #1: maths.addTest. The tests can be executed from the cmake_fundamentals/build directory by simply running ctest: $ ctest Installing: /home/user/install/lib/cmake/Maths/MathsConfigVersion.cmake Installing: /home/user/install/lib/cmake/Maths/MathsConfig.cmake Installing: /home/user/install/lib/cmake/Maths/Maths-debug.cmake Installing: /home/user/install/lib/cmake/Maths/Maths.cmake Installing: /home/user/install/lib/libMaths.a $ cmake -build code/build -j4 -target install $ cmake -build cmake_fundamentals/build -j8 -target install Here are the commands: $ cmake -DCMAKE_BUILD_TYPE:STRING=Release -DBUILD_SHARED_LIBS:BOOL=OFF -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/install -B cmake_fundamentals/build -S cmake_fundamentals Īs a refresher let’s configure and build the projects and run the test suite. We ended up with a two-project setup, where the cmake_fundamentals project would build only the Maths library and a test_project would use an installed Maths library in a trivial executable. Just a quick reminder of where we left of in the previous post. In the usual fashion let’s try to take the quickest path to our goal and taclke any issues as they show up. Rather than postponing it any further, I will present the missing information in this post. I did however skip the topic of shared libraries.
In the previous part of the series, I discussed how to handle project installation.