How to install Gtest On MacOS and Xcode(如何在MacOS上安装gtest以及XCode使用gtest)

How to install Gtest On MacOS

git clone https://github.com/google/googletest.git 
cd googletest
makedir build && cd build
cmake -DCMAKE_CXX_STANDARD=17 ..
make
sudo make install


######test
mkdir gtestDemo
vi CMakeList.txt
echo “export CPLUS_INCLUDE_PATH=/usr/local/include" >> ~/.zshrc  

echo "export LIBRARY_PATH=/usr/local/lib" >> ~/.zshrc  
source ~/.zshrc #tell terminal there are new variables  
#CMakeList.txt
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 17)
project(demo)
find_package(GTEST REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES})
#main.cpp
#include <iostream>
#include <gtest/gtest.h>
int add(int a, int b) {
    return a + b;
}
int sub(int a, int b) {
    return a - b;
}
// case1
TEST(test, c1) {
    EXPECT_EQ(3, add(1, 2));
    EXPECT_EQ(12, add(2, 6));
}
// case2
TEST(test, c2) {
    EXPECT_EQ(-1, sub(1, 2));
}
GTEST_API_ int main(int argc, char ** argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

cmake .
make
./demo
#you can also use the g++ command
g++ -std=c++11 -stdlib=libc++ main.cpp -o main

Xcode 中的使用

click the workspace and set like this, but the path you should replace by yourself.(if you install follow the step,you could not change it.)
How to install Gtest On MacOS and Xcode(如何在MacOS上安装gtest以及XCode使用gtest)
How to install Gtest On MacOS and Xcode(如何在MacOS上安装gtest以及XCode使用gtest)
How to install Gtest On MacOS and Xcode(如何在MacOS上安装gtest以及XCode使用gtest)

Conference

http://hack.limbicmedia.ca/installing-google-test/
https://blog.csdn.net/v55538679/article/details/77824214

上一篇:Xcode 11无法成功安装Cocoapods的原因和解决方案: mkmf.rb can't find header files for ruby at xxx


下一篇:Git学习系列(一)安装Git