Programing

Linux에서 googleTest를 공유 라이브러리로 설정하는 방법

lottogame 2020. 10. 7. 07:13
반응형

Linux에서 googleTest를 공유 라이브러리로 설정하는 방법


Debian은 더 이상 gTest 용으로 사전 컴파일 된 패키지를 제공하지 않습니다. 프레임 워크를 프로젝트의 메이크 파일에 통합 할 것을 제안합니다. 하지만 내 makefile을 깨끗하게 유지하고 싶습니다. 라이브러리에 연결할 수 있도록 이전 버전 (<1.6.0)과 같이 gTest를 설정하려면 어떻게해야합니까?


시작하기 전에 Google의이 메모를 읽고 이해했는지 확인하십시오 ! 이 튜토리얼은 gtest를 쉽게 사용할 수 있도록 해주지 만 끔찍한 버그 가 발생할 수 있습니다 .

1. googletest 프레임 워크 가져 오기

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

또는 손으로 가져 오십시오 . 이 작은 How-to는 유지하지 않을 것이므로 우연히 발견하고 링크가 오래 되었다면 자유롭게 편집하십시오.

2. Google 테스트 압축 풀기 및 빌드

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

3. 시스템에 헤더와 라이브러리를 "설치"합니다.

이 단계는 배포판마다 다를 수 있으므로 올바른 디렉토리에 헤더와 라이브러리를 복사해야합니다. 나는 데비안의 이전 gtest 라이브러리어디에 있는지 확인함으로써 이것을 달성했습니다 . 하지만 더 나은 방법이 있다고 확신합니다. 참고 : make install위험하며 지원되지 않습니다.

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

4. 링커의 캐시 업데이트

... GNU 링커가 libs를 알고 있는지 확인하십시오.

$ sudo ldconfig -v | grep gtest

출력이 다음과 같은 경우 :

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

, 모든 것이 좋습니다.

이제 gTestframework를 사용할 준비가되었습니다. -lgtest링커 플래그 로 설정 하고 선택적으로 자체 테스트 메인 루틴을 작성하지 않은 경우 명시 적 -lgtest_main플래그 로 설정하여 프로젝트를 라이브러리에 연결하는 것을 잊지 마십시오 .

여기에서 프레임 워크에 대한 Google 문서 로 이동하여 작동 방식을 배울 수 있습니다. 즐거운 코딩 되세요!

편집 : 이것은 OS X에서도 작동합니다! "OS X에서 googleTest를 올바르게 설정하는 방법"을 참조하십시오.


우분투 사용자를 위해 특별히 대답하겠습니다. 먼저 gtest 개발 패키지를 설치하십시오.

sudo apt-get install libgtest-dev

이 패키지는 소스 파일 만 설치합니다. 필요한 라이브러리 파일을 만들려면 코드를 직접 컴파일해야합니다. 이러한 소스 파일은 / usr / src / gtest에 있어야합니다. 이 폴더를 찾아 cmake를 사용하여 라이브러리를 컴파일합니다.

sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make

# copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib

이제 gtest를 사용하는 프로그램을 컴파일하려면 다음과 연결해야합니다.

-lgtest -lgtest_main

이것은 Ubuntu 14.04LTS에서 완벽하게 작동했습니다.


정상적인 "make install"이 제거되었고 cmake를 사용하지 않았기 때문에 이것을 알아내는 데 시간이 걸렸습니다. 여기에 공유 할 경험이 있습니다. 직장에서는 Linux에서 루트 액세스 권한이 없으므로 홈 디렉토리 아래에 Google 테스트 프레임 워크를 설치했습니다 ~/usr/gtest/..

~ / usr / gtest /에 패키지를 공유 라이브러리로 설치하고 샘플 빌드도 함께 설치하려면 :

$ mkdir ~/temp
$ cd ~/temp
$ unzip gtest-1.7.0.zip 
$ cd gtest-1.7.0
$ mkdir mybuild
$ cd mybuild
$ cmake -DBUILD_SHARED_LIBS=ON -Dgtest_build_samples=ON -G"Unix Makefiles" ..
$ make
$ cp -r ../include/gtest ~/usr/gtest/include/
$ cp lib*.so ~/usr/gtest/lib

설치를 검증하려면 다음 test.c를 간단한 테스트 예제로 사용하십시오.

    #include <gtest/gtest.h>
    TEST(MathTest, TwoPlusTwoEqualsFour) {
        EXPECT_EQ(2 + 2, 4);
    }

    int main(int argc, char **argv) {
        ::testing::InitGoogleTest( &argc, argv );
        return RUN_ALL_TESTS();
    }

컴파일하기:

    $ export GTEST_HOME=~/usr/gtest
    $ export LD_LIBRARY_PATH=$GTEST_HOME/lib:$LD_LIBRARY_PATH
    $ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp 

CMake를 사용 ExternalProject_Add하는 경우 여기에 설명 된대로 사용할 수 있습니다 .

이렇게하면 저장소에 gtest 소스 코드를 보관하거나 어디에나 설치할 필요가 없습니다. 빌드 트리에 자동으로 다운로드되고 빌드됩니다.


I was similarly underwhelmed by this situation and ended up making my own Ubuntu source packages for this. These source packages allow you to easily produce a binary package. They are based on the latest gtest & gmock source as of this post.

Google Test DEB Source Package

Google Mock DEB Source Package

To build the binary package do this:

tar -xzvf gtest-1.7.0.tar.gz
cd gtest-1.7.0
dpkg-source -x gtest_1.7.0-1.dsc
cd gtest-1.7.0
dpkg-buildpackage

It may tell you that you need some pre-requisite packages in which case you just need to apt-get install them. Apart from that, the built .deb binary packages should then be sitting in the parent directory.

For GMock, the process is the same.

As a side note, while not specific to my source packages, when linking gtest to your unit test, ensure that gtest is included first (https://bbs.archlinux.org/viewtopic.php?id=156639) This seems like a common gotcha.


Just in case somebody else gets in the same situation like me yesterday (2016-06-22) and also does not succeed with the already posted approaches - on Lubuntu 14.04 it worked for me using the following chain of commands:

git clone https://github.com/google/googletest
cd googletest
cmake -DBUILD_SHARED_LIBS=ON .
make
cd googlemock
sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/
sudo ldconfig

This answer from askubuntu is what worked for me. Seems simpler than other options an less error-prone, since it uses package libgtest-dev to get the sources and builds from there: https://askubuntu.com/questions/145887/why-no-library-files-installed-for-google-test?answertab=votes#tab-top

Please refer to that answer, but just as a shortcut I provide the steps here as well:

sudo apt-get install -y libgtest-dev
sudo apt-get install -y cmake
cd /usr/src/gtest
sudo cmake .
sudo make
sudo mv libg* /usr/lib/

After that, I could build my project which depends on gtest with no issues.


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.


This will build and install both gtest and gmock 1.7.0:

mkdir /tmp/googleTestMock
tar -xvf googletest-release-1.7.0.tar.gz -C /tmp/googleTestMock
tar -xvf googlemock-release-1.7.0.tar.gz -C /tmp/googleTestMock
cd /tmp/googleTestMock
mv googletest-release-1.7.0 gtest
cd googlemock-release-1.7.0
cmake -DBUILD_SHARED_LIBS=ON .
make -j$(nproc)
sudo cp -a include/gmock /usr/include
sudo cp -a libgmock.so libgmock_main.so /usr/lib/
sudo cp -a ../gtest/include/gtest /usr/include
sudo cp -a gtest/libgtest.so gtest/libgtest_main.so /usr/lib/
sudo ldconfig

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)

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/

참고URL : https://stackoverflow.com/questions/13513905/how-to-set-up-googletest-as-a-shared-library-on-linux

반응형