Установка google test linux

Cartexius / install_gtest_ubuntu.md

sudo apt-get install libgtest-dev

sudo apt-get install cmake # install cmake

sudo cmake CMakeLists.txt

sudo cp *.a /usr/lib

sudo ln -s /usr/lib/libgtest.a /usr/local/lib/gtest/libgtest.a

sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/gtest/libgtest_main.a

This comment has been minimized.

Copy link Quote reply

basil59 commented Apr 10, 2018

I think you need to create the gtest directory before creating the symbolic links

This comment has been minimized.

Copy link Quote reply

pareshBloomA commented Jul 18, 2018

@basil59, I agree with you, but looking at www.askubuntu.com stack exchange reference below shows that libgtest-dev is now stored in /usr/src/googletest/googletest . So it might be a good idea to make symbolic links to googletest folder. Of course, I could be wrong, and this would be dependent on the package you are trying to use gtest for, and may require you to just use /usr/local/lib/gtest .

Reference to answer for 17.04 and 18.04 libgtest-dev: https://askubuntu.com/a/145913

This comment has been minimized.

Copy link Quote reply

StrayDragon commented Jan 28, 2019

@basil59, I agree with you, but looking at www.askubuntu.com stack exchange reference below shows that libgtest-dev is now stored in /usr/src/googletest/googletest . So it might be a good idea to make symbolic links to googletest folder. Of course, I could be wrong, and this would be dependent on the package you are trying to use gtest for, and may require you to just use /usr/local/lib/gtest .

Reference to answer for 17.04 and 18.04 libgtest-dev: https://askubuntu.com/a/145913

Thank for your summary, It works very well. 😄

Источник

dlime / CMakeLists.txt

cmake_minimum_required ( VERSION 3.5)
project (example LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED ON )
find_package (GTest REQUIRED )
include_directories ( $ )
add_executable (example main.cpp)
target_link_libraries (example $ $ )
git clone https://github.com/google/googletest.git cd googletest mkdir build && cd build cmake .. -DBUILD_SHARED_LIBS=ON -DINSTALL_GTEST=ON -DCMAKE_INSTALL_PREFIX:PATH=/usr make -j8 sudo make install sudo ldconfig
# include gtest/gtest.h >
TEST (test_case, example_test)
<
EXPECT_EQ ( 2 , 2 );
>
int main ( int argc, char **argv)
<
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
>

This comment has been minimized.

Copy link Quote reply

leannejdong commented Jul 5, 2020

Hi, I am on ubuntu20.04. I wonder what is your CMakeLists.txt look like? Do you have ant test cases?
I spent hours but could not get gtest working by trying many tricks.

This comment has been minimized.

Copy link Quote reply

dlime commented Jul 6, 2020

Hi, I am on ubuntu20.04. I wonder what is your CMakeLists.txt look like? Do you have ant test cases?
I spent hours but could not get gtest working by trying many tricks.

Hello! I’ve just tested this Gist on Ubuntu 20.04 and I got successful results.
Use this as an example CMakeLists.txt

This comment has been minimized.

Copy link Quote reply

This comment has been minimized.

Copy link Quote reply

dlime commented Jul 7, 2020

  1. gist script will clone Gtest source code, build it, install it in default Ubuntu libraries directory
  2. the project specific CMakeLists.txt will set Gtest as required library (it will look in the above mentioned Ubuntu directory) and link the public headers to your project executable
  3. in main.cpp Gtest header is imported and used in the example test
    let me know if it was clear:)

This comment has been minimized.

Copy link Quote reply

This comment has been minimized.

Copy link Quote reply

skwasniak commented Jan 20, 2021

Not sure why but for me it did not work with the share lib flag.

So what I did was (note the -DBUILD_SHARED_LIBS=OFF ):

. furthermore my CMakeLists.txt looks a little different, as I took the one from the official CMake documentation (https://cmake.org/cmake/help/v3.6/module/FindGTest.html) :

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Источник

Установка google test linux

Google Test (referred to as gtest) project home page (please click here). Click [Clone or Download] and then click [Download Zip] to download a file called Googletest-Master.zip. After downloading this file, in my system

/ downloads / directory, use the following command to implement the decompression:

After decompression, get a new directory Googletest, which has an installation guide in the ReadMe.md file in this directory. I chose the CMAKE-based installation, just create a directory and run CMAKE, you can get a series of files you need when you really build (such as makefile). If you are using an APT-GET-based Linux distribution, you can install CMAKE using the following command:

After CMAKE, run the following command to generate the auxiliary file required to build GTEST:

After running, the corresponding Makefile file will be generated in the current directory, this time you can build Google Test, the command is as follows:

On my system,sudo make installWilllibgtest.aThis file is placed/usr/local/lib/Under this directory. Now, you can use the GTEST framework to write test code, a very simple example is as follows:

Save the above code in file gtest.cpp, use the following command to compile and link

g++ gtest.o /usr/local/lib/libgtest.a -lpthread

This will generate a file named a.out in the current directory. If you perform this file, you can see the effect of Google Test, and the output content on my system is as follows:

If the code is successfully compiled and running, that is, the installation is successful.

Источник

Getting started with Google Test (GTest) on Ubuntu

by Erik Smistad · Published July 5, 2012 · Updated July 5, 2012

Google test is a framework for writing C++ unit tests. In this short post, I explain how to set it up in Ubuntu.

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 cmake CMakeLists.txt sudo make # copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder sudo cp *.a /usr/lib

Lets say we now want to test the following simple squareRoot function:

In the following code, we create two tests that test the function using a simple assertion. There exists many other assertion macros in the framework (see http://code.google.com/p/googletest/wiki/Primer#Assertions). The code contains a small main function that will run all of the tests automatically. Nice and simple!

The next step is to compile the code. I’ve set up a small CMakeLists.txt file below to compile the tests. This file locates the google test library and links it with the test application. Note that we also have to link to the pthread library or the application won’t compile.

cmake_minimum_required(VERSION 2.6) # Locate GTest find_package(GTest REQUIRED) include_directories($) # Link runTests with what we want to test and the GTest and pthread library add_executable(runTests tests.cpp) target_link_libraries(runTests $ pthread)

Compile and run the tests:

cmake CMakeLists.txt make ./runTests

Have fun testing! You can download all of the code above at my Github page: https://github.com/smistad/GTest

References

Memory-mapped files using the boost library

October 11, 2012

by Erik Smistad · Published October 11, 2012

Measuring runtime in milliseconds using the C++ 11 chrono library

by Erik Smistad · Published July 13, 2012

OpenCL-OpenGL interoperability problems on AMD GPUs and Linux

by Erik Smistad · Published August 22, 2013 · Last modified May 28, 2016

79 Responses

undefined reference to main…

I have gtest (mostly?) installed on my Raspberry PI 3 Model B+
The problem came in when I added “EXPECT_CALL” to the program.
Before that other things worked well.
Hope you can help!
The command line:
g++ gmockTest.cpp -std=c++11 -lgtest -lgtest_main -lgmock -pthread -o test
Got linker error:
/usr/bin/ld: /tmp/cc30mKHm.o: in function `testing::internal::FunctionMocker , std::__cxx11::basic_string )>::AddNewExpectation(char const*, int, std::__cxx11::basic_string const&, std::tuple >, testing::Matcher > > const&)’:
gmockTest.cpp:(.text._ZN7testing8internal14FunctionMockerIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE[_ZN7testing8internal14FunctionMockerIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE]+0xcc): undefined reference to `testing::Expectation::Expectation(std::shared_ptr const&)’

HI when I’m executing make command its Showing no rule to make target
error .How to do it makefile Could you please explain anyone??

how to write gtest in cpp if there is no return value from the function.
please show the sample for the same

Modern CMake suggests never using include_directories() as you are operating on the directory level. It’s much better operate just on the targets. See Youtube video (at:18:26) “C++Now 2017: Effective CMake” by Daniel Pfeifer

Hi, thanks for this quick guidance. However, I constantly meeting this same error:
tests.cpp:(.text+0xf8): undefined reference to `testing::Message::Message()’
anything I did wrong?

How to get html code coverage report for this tutorial…please help me

you can use “sudo make install” instead of copying the lib

How would this work on Linux Mint OS? I’ve been playing with it, and it doesn’t work the same.

It works on Ubuntu 16.04 if you clone the gtest repo, build it, and install the library manually
git clone https://github.com/google/googletest
mkdir -p googletest/build
cd googletest/build
cmake ..
make
sudo make install

Gtest is running in ubuntu 16.04 but not in ubuntu 18.04. Could you please tell what will be the issue.

[==========] Running 10 tests from 5 test cases.
[———-] Global test environment set-up.
[———-] 5 tests from AgentAppTest
[ RUN ] AgentAppTest.test_getInstance

When 1st test case encountered it will stop running.

Add gtest_main to the target_link_libraries so you can remove your main method in the test code. Like so:

`target_link_libraries(runTests $ gtest_main pthread)`

Great! I didn’t know that, thanks for the tip

Very helpful guide and compensates for Google’s near universal-inability to provide decent docs or getting started guides for its open source projects. Thanks very much for this!

compilation errors have occurred while I build the FAST library on ubuntu.

/home/kerax/Desktop/FAST/src/source/CL/cl.hpp:4755:28: warning: ignoring attributes on template argument ‘cl_int ’ [-Wignored-attributes]
VECTOR_CLASS* binaryStatus = NULL,
^
In file included from /home/kerax/Desktop/FAST/src/source/CL/OpenCL.hpp:5:0,
from /home/kerax/Desktop/FAST/src/source/FAST/RuntimeMeasurementManager.hpp:6,
from /home/kerax/Desktop/FAST/src/source/FAST/ExecutionDevice.hpp:5,
from /home/kerax/Desktop/FAST/src/source/FAST/Data/DataObject.hpp:6,
from /home/kerax/Desktop/FAST/src/source/FAST/AffineTransformation.hpp:4,
from /home/kerax/Desktop/FAST/src/source/FAST/Visualization/View.hpp:5,
from /home/kerax/Desktop/FAST/src/source/FAST/Visualization/WindowWidget.hpp:13,
from /home/kerax/Desktop/FAST/src/source/FAST/Visualization/WindowWidget.cpp:1:
/home/kerax/Desktop/FAST/src/source/CL/cl.hpp:4755:28: warning: ignoring attributes on template argument ‘cl_int ’ [-Wignored-attributes]
VECTOR_CLASS* binaryStatus = NULL,
^
CMakeFiles/Makefile2:187: recipe for target ‘CMakeFiles/FAST.dir/all’ failed
make[1]: *** [CMakeFiles/FAST.dir/all] Error 2
Makefile:129: recipe for target ‘all’ failed
make: *** [all] Error 2

I just see two warnings, the actual error must be somewhere else in the output. If you need any help compiling FAST, please use the gitter chatrom instead: https://gitter.im/smistad/FAST

Crazy. The ubuntu package is not even compiled…

How to add the gtest libraries using g++?

The tutorial for gtest is good, but the example isn’t well chosen. It’s not a good idea to return -1 for bad values, so why give that as an example? Better to show that you expect the function to return NaN for negative numbers, and infinity for positive numbers. That would actually be useful.

If you want to write your own function, do hypotenuse rather than messing with a square root function that works!

great thanks !! it’s was very useful

That’s really helpful, thanks!

It works perfectly on Raspberry Pi 3 as well. Thank you so much 🙂

Hello,
If I separate the main test from the code, it does not execute the tests and says 0 test executed. Any idea?.

If you want to split the tests over multiple files you need to add the files to the line add_executable(runTests tests.cpp newFile.cpp) in CMakeLists.txt

very helpful, thanks

Very helpful and effective. Thanks a lot.

Thank You. I had struggled months ago trying to get going with GoogleTest.
Your example gave me reassurance that the install went correctly.
I used it on Centos 7
# s/apt-get/yum
Again thanks for helping some many others.

Hello Erik,
I don’t find the gmock after following your setup instructions here(by installing libgtest-dev). How can I add gmock seamlessly and run successfully? I tried but I am keeping getting some error. So I have to uninstall libgtest-dev.
Thanks

Great. Thanks a lot for this helpful info!!

Thanks…It helped me a lot.

Thanks very much..

Hi ..
I fallow the same steps but it was given error like .

/usr/include/c++/4.6/bits/stl_bvector.h:871:45: error: ‘((std::vector *)this)->std::vector . std::_Bvector_base ::_M_allocate’ cannot be used as a function
/usr/include/c++/4.6/bits/stl_bvector.h:876:7: error: ‘difference_type’ was not declared in this scope
make[2]: *** [CMakeFiles/runTests.dir/tests.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2

Please some one help to resolve this.

correct the spelling of SquareRoot in tests.cpp code.

Worked out of the box! Nice! What about Google Mock? Any instructions for that? I understand it’s now under the Test fold. Lastly, I did this instead:
mkdir /usr/local/lib/gtest
cp *.a /usr/local/lib/gtest
ln -s /usr/local/lib/gtest/libgtest.a /usr/lib/libgtest.a
ln -s /usr/local/lib/gtest/libgtest_main.a /usr/lib/libgtest_main.a
That way, the libs are contained in my local lib.

Haven’t tried google mock. Personally, I have moved on to using catch testing framework instead https://github.com/philsquared/Catch

Hi,
How to create link to the pthread library on ubuntu?
Regards

That is what the last line in the cmake file does: target_link_libraries(runTests $ pthread)

If you are compiling directly with gcc you can simply add -lpthread

Thanks! Solved my problem

Great guide for beginners, thank you!

Thanks Erik, helped me get started using gTest for my project.

Really clear, simple, short guide that did exactly what I wanted. Thank you!

Hi,
thanks for this guide. It is working now.

Thank you very much, this was very helpful!

At the last step, when I type cmake CMakeLists.txt, I get these errors:

CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:91 (MESSAGE):
Could NOT find GTest (missing: GTEST_INCLUDE_DIR)
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:252 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-2.8/Modules/FindGTest.cmake:150 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:4 (find_package)

— Configuring incomplete, errors occurred!

Do you know how to solve this problem? Thanks.

I have solved this problem myself.

Previously I was downloading Google test from
https://code.google.com/p/googletest/
and got the error.

Now I am doing:
sudo apt-get install libgtest-dev
and exactly follow your steps. Now things work well.

my folder structure is as below :

deepak@deepak:/usr/src/gtest$ ll
total 1256
drwxr-xr-x 6 root root 4096 Sep 18 18:29 ./
drwxr-xr-x 7 root root 4096 Sep 18 18:03 ../
drwxr-xr-x 2 root root 4096 Sep 18 18:03 cmake/
-rw-r–r– 1 root root 11730 Sep 18 18:12 CMakeCache.txt
drwxr-xr-x 7 root root 4096 Sep 18 18:12 CMakeFiles/
-rw-r–r– 1 root root 1552 Sep 18 18:12 cmake_install.cmake
-rw-r–r– 1 root root 8664 Apr 16 2011 CMakeLists.txt
-rw-r–r– 1 root root 1218562 Sep 18 18:12 libgtest.a
-rw-r–r– 1 root root 3726 Sep 18 18:12 libgtest_main.a
-rw-r–r– 1 root root 5903 Sep 18 18:12 Makefile
drwxr-xr-x 2 root root 4096 Sep 18 18:03 src/
drwxr-xr-x 3 root root 4096 Sep 19 14:48 unittest/
deepak@deepak:/usr/src/gtest$ cd unittest/
deepak@deepak:/usr/src/gtest/unittest$ ll
total 52
drwxr-xr-x 3 root root 4096 Sep 19 14:48 ./
drwxr-xr-x 6 root root 4096 Sep 18 18:29 ../
-rw-r–r– 1 root root 12273 Sep 19 12:11 CMakeCache.txt
drwxr-xr-x 9 root root 4096 Sep 19 14:48 CMakeFiles/
-rw-r–r– 1 root root 1579 Sep 18 18:40 cmake_install.cmake
-rw-r–r– 1 root root 296 Sep 19 14:48 CMakeLists.txt
-rw-r–r– 1 root root 332 Sep 19 13:00 CMakeLists.txt

-rw-r–r– 1 root root 4574 Sep 19 14:48 Makefile
-rw-r–r– 1 root root 499 Sep 19 14:22 tests.cpp
-rw-r–r– 1 root root 175 Sep 18 18:33 whattotest.cpp

I got some reference error while execute “make” command.
Error :
Linking CXX executable runTests
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, int const&, double const&)’:
tests.cpp:(.text._ZN7testing8internal11CmpHelperEQIidEENS_15AssertionResultEPKcS4_RKT_RKT0_[testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, int const&, double const&)]+0x9b): undefined reference to `testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)’
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, double const&, double const&)’:
tests.cpp:(.text._ZN7testing8internal11CmpHelperEQIddEENS_15AssertionResultEPKcS4_RKT_RKT0_[testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, double const&, double const&)]+0x99): undefined reference to `testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)’
collect2: ld returned 1 exit status
make[2]: *** [runTests] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2

Please help me in this.

Thanks,
Deepakgiri Aparnathi

I don’t know. I haven’t seen this error before.

Источник

Читайте также:  Изменить irq windows 10
Оцените статью