Last active
May 12, 2023 13:36
-
-
Save dlime/313f74fd23e4267c4a915086b84c7d3d to your computer and use it in GitHub Desktop.
Install Google Test and Google Mock on Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(${GTEST_INCLUDE_DIRS}) | |
add_executable(example main.cpp) | |
target_link_libraries(example ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd ~ | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
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
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(${GTEST_INCLUDE_DIRS})
add_executable(example main.cpp)
target_link_libraries(example ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
and main.cpp
#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();
}
HI Dario, thanks for trying that.
I don't understand but it magically works:)
Greeting, Leanne
…On Mon, Jul 6, 2020 at 6:11 AM Dario Limongi ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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
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(${GTEST_INCLUDE_DIRS})
add_executable(example main.cpp)
target_link_libraries(example ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
and main.cpp
#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();
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/313f74fd23e4267c4a915086b84c7d3d#gistcomment-3365445>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABMLMAJUPZA5D3ZTOVXZFMLR2GPMXANCNFSM4OQW7XCA>
.
- gist script will clone Gtest source code, build it, install it in default Ubuntu libraries directory
- 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 - in
main.cpp
Gtest header is imported and used in the example test
let me know if it was clear:)
Hi, thank you for the information.
What exactly is the default ubuntu libraries directory?
As you know, I follow
https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
https://gist.github.com/Cartexius/4c437c084d6e388288201aadf9c8cdd5
and I used the CMakeLists.txt you provided.
It works fine so far.
Not sure what will be any issues. Removing everything is a mess again ...
…On Tue, Jul 7, 2020 at 3:41 AM Dario Limongi ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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:)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/313f74fd23e4267c4a915086b84c7d3d#gistcomment-3366508>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABMLMAP5T3Z7NCGUVLAWR33R2LGSNANCNFSM4OQW7XCA>
.
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
):
git clone https://github.com/google/googletest.git
cd googletest
mkdir build && cd build
cmake .. -GNinja -DBUILD_SHARED_LIBS=OFF -DINSTALL_GTEST=ON -DCMAKE_INSTALL_PREFIX:PATH=/usr
cmake --build .
sudo cmake --install .
sudo ldconfig
...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) :
enable_testing()
find_package(GTest REQUIRED)
add_executable(foo foo.cc)
target_link_libraries(foo GTest::GTest GTest::Main)
add_test(AllTestsInFoo foo)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.