How to set up googleTest as a shared library on Linux

后端 未结 12 2427
北荒
北荒 2020-11-28 01:04

Debian does not provide any precompiled packages for gTest anymore. They suggest you integrate the framework into your project\'s makefile. But I want to keep my makefile cl

相关标签:
12条回答
  • 2020-11-28 01:48

    For 1.8.1 based on @ManuelSchneid3r 's answer I had to do:

    wget github.com/google/googletar xf release-1.8.1.tar.gz 
    tar xf release-1.8.1.tar.gz
    cd googletest-release-1.8.1/
    cmake -DBUILD_SHARED_LIBS=ON .
    make
    

    I then did make install which seemed to work for 1.8.1, but following @ManuelSchneid3r it would mean:

    sudo cp -a googletest/include/gtest /usr/include
    sudo cp -a googlemock/include/gmock /usr/include
    sudo cp `find .|grep .so$` /usr/lib/
    
    0 讨论(0)
  • 2020-11-28 01:52

    Before you start make sure your have read and understood this note from Google! This tutorial makes using gtest easy, but may introduce nasty bugs.

    1. Get the googletest framework

    wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz
    

    Or get it by hand. I won't maintain this little How-to, so if you stumbled upon it and the links are outdated, feel free to edit it.

    2. Unpack and build google test

    tar xf release-1.8.0.tar.gz
    cd googletest-release-1.8.0
    cmake -DBUILD_SHARED_LIBS=ON .
    make
    

    3. "Install" the headers and libs on your system.

    This step might differ from distro to distro, so make sure you copy the headers and libs in the correct directory. I accomplished this by checking where Debians former gtest libs were located. But I'm sure there are better ways to do this. Note: make install is dangerous and not supported

    sudo cp -a googletest/include/gtest /usr/include
    sudo cp -a googlemock/gtest/libgtest_main.so googlemock/gtest/libgtest.so /usr/lib/
    

    4. Update the cache of the linker

    ... and check if the GNU Linker knows the libs

    sudo ldconfig -v | grep gtest
    

    If the output looks like this:

    libgtest.so.0 -> libgtest.so.0.0.0
    libgtest_main.so.0 -> libgtest_main.so.0.0.0
    

    then everything is fine.

    gTestframework is now ready to use. Just don't forget to link your project against the library by setting -lgtest as linker flag and optionally, if you did not write your own test mainroutine, the explicit -lgtest_main flag.

    From here on you might want to go to Googles documentation, and the old docs about the framework to learn how it works. Happy coding!

    Edit: This works for OS X too! See "How to properly setup googleTest on OS X"

    0 讨论(0)
  • 2020-11-28 01:52

    Update for Debian/Ubuntu

    Google Mock (package: google-mock) and Google Test (package: libgtest-dev) have been merged. The new package is called googletest. Both old names are still available for backwards compatibility and now depend on the new package googletest.

    So, to get your libraries from the package repository, you can do the following:

    sudo apt-get install googletest -y
    cd /usr/src/googletest
    sudo mkdir build
    cd build
    sudo cmake ..
    sudo make
    sudo cp googlemock/*.a googlemock/gtest/*.a /usr/lib
    

    After that, you can link against -lgmock (or against -lgmock_main if you do not use a custom main method) and -lpthread. This was sufficient for using Google Test in my cases at least.

    If you want the most current version of Google Test, download it from github. After that, the steps are similar:

    git clone https://github.com/google/googletest
    cd googletest
    sudo mkdir build
    cd build
    sudo cmake ..
    sudo make
    sudo cp lib/*.a /usr/lib
    

    As you can see, the path where the libraries are created has changed. Keep in mind that the new path might be valid for the package repositories soon, too.

    Instead of copying the libraries manually, you could use sudo make install. It "currently" works, but be aware that it did not always work in the past. Also, you don't have control over the target location when using this command and you might not want to pollute /usr/lib.

    0 讨论(0)
  • 2020-11-28 01:54

    The following method avoids manually messing with the /usr/lib directory while also requiring minimal change in your CMakeLists.txt file. It also lets your package manager cleanly uninstall libgtest-dev.

    The idea is that when you get the libgtest-dev package via

    sudo apt install libgtest-dev
    

    The source is stored in location /usr/src/googletest

    You can simply point your CMakeLists.txt to that directory so that it can find the necessary dependencies

    Simply replace FindGTest with add_subdirectory(/usr/src/googletest gtest)

    At the end, it should look like this

    add_subdirectory(/usr/src/googletest gtest)
    target_link_libraries(your_executable gtest)
    
    0 讨论(0)
  • 2020-11-28 01:57

    This will install google test and mock library in Ubuntu/Debian based system:

    sudo apt-get install google-mock
    

    Tested in google cloud in debian based image.

    0 讨论(0)
  • 2020-11-28 01:59

    Let me answer this specifically for ubuntu users. First start by installing the gtest development package.

    sudo apt-get install libgtest-dev
    

    Note that this package only install source files. You have to compile the code yourself to create the necessary library files. These source files should be located at /usr/src/gtest. Browse to this folder and use cmake to compile the library:

    sudo apt-get install cmake # install cmake
    cd /usr/src/gtest
    sudo mkdir build
    cd build
    sudo cmake ..
    sudo make
    sudo make install
    

    Now to compile your programs that uses gtest, you have to link it with:

    -lgtest -lgtest_main -lpthread
    

    This worked perfectly for me on Ubuntu 14.04LTS.

    0 讨论(0)
提交回复
热议问题