A Comprehensive Guide to Setting Up GoogleTest as a Shared Library on Linux

Dec 11, 2025 · Programming · 7 views · 7.8

Keywords: GoogleTest | Shared Library | Linux Setup | CMake | Unit Testing

Abstract: This article provides a detailed guide for configuring GoogleTest as a shared library on Linux systems. Addressing the issue where distributions like Debian no longer offer precompiled packages, it outlines a systematic approach based on official best practices, covering steps from source acquisition, compilation, and installation to linking configuration. The discussion includes the use of CMake build system, differences between shared and static libraries, and how to avoid common pitfalls. It also compares various installation methods and offers verification techniques to ensure successful setup, helping developers maintain clean project build files.

Introduction and Background

In the realm of C++ unit testing, GoogleTest (gtest) is a widely adopted framework. However, since version 1.6.0, many Linux distributions, such as Debian, have ceased providing precompiled gtest packages, posing configuration challenges for developers. This article aims to deliver a system-level solution by setting up GoogleTest as a shared library, enabling projects to use it via simple linker flags (e.g., -lgtest) while keeping project Makefiles clean.

Acquiring the GoogleTest Framework

First, obtain the GoogleTest source code from the official GitHub repository. It is recommended to use wget to download a specific version (e.g., 1.8.0) to ensure compatibility and stability. The command is: wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz. After downloading, extract it with tar: tar xf release-1.8.0.tar.gz, and navigate into the extracted directory: cd googletest-release-1.8.0. Note that one should periodically check the official releases page for updates.

Compilation and Building as a Shared Library

Use the CMake build system to compile GoogleTest into a shared library. In the source directory, run: cmake -DBUILD_SHARED_LIBS=ON .. Here, -DBUILD_SHARED_LIBS=ON is a key parameter that instructs CMake to generate shared libraries (.so files) instead of static ones. Then, execute make to compile. This process produces library files such as libgtest.so and libgtest_main.so. Shared libraries offer benefits like code sharing across multiple programs, reducing memory usage, but require careful version management and dependency handling.

Installing Library Files and Headers

After compilation, install the library files and headers to system directories for global access. One method is manual copying: place headers in /usr/include and libraries in /usr/lib. For example: sudo cp -a googletest/include/gtest /usr/include and sudo cp -a googlemock/gtest/libgtest_main.so googlemock/gtest/libgtest.so /usr/lib/. A more recommended approach is using make install, but note that prior to version 1.11, this could be risky and is not officially supported. Always consult distribution documentation to confirm correct directories before installation.

Updating Linker Cache and Verification

Post-installation, update the system's linker cache so the dynamic linker recognizes the new libraries. Run sudo ldconfig -v and check if the output includes gtest libraries, e.g., libgtest.so.0 -> libgtest.so.0.0.0. This indicates successful registration. Verification is crucial to prevent future linking errors.

Using GoogleTest in Projects

Once configured, simply add linker flags to use GoogleTest in C++ projects. For instance, include -lgtest to link the main library, and if a custom main() function is not written, add -lgtest_main. Additionally, since GoogleTest may depend on pthread, it is advisable to link -lpthread as well. Example compile command: g++ -o mytest mytest.cpp -lgtest -lgtest_main -lpthread. This keeps project Makefiles clean without embedding gtest source code.

Supplementary Methods and Considerations

Beyond the primary method, Ubuntu users can install the development package via sudo apt-get install libgtest-dev, but note this only provides source code, requiring manual compilation. Navigate to /usr/src/gtest and build with CMake: sudo cmake .. && sudo make && sudo make install. However, this approach may be less flexible than building from source. Regardless of the method, always review Google's official FAQ to understand why precompiled installations are discouraged, avoiding issues like ABI incompatibility or debugging difficulties. For example, official documentation warns that precompiled libraries can lead to hard-to-debug linking errors, especially across different compiler versions.

Conclusion and Best Practices

Configuring GoogleTest as a shared library is an effective solution, particularly for scenarios requiring clean project build systems. Based on the best answer, this article presents a complete workflow from source acquisition to project integration. Key steps include: building shared libraries with CMake, proper file installation, updating the linker cache, and verification. Developers are encouraged to always refer to official documentation and adapt steps based on specific distributions. For instance, similar methods apply on macOS, but directory structures may differ. This approach ensures reliable and maintainable unit testing, enhancing development efficiency.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.