简体   繁体   English

如何使用自制软件在 Mac OS X 上安装 GTest?

[英]How to install GTest on Mac OS X with homebrew?

I'm trying to install gtest with my packet manager Home Brew but there is no repository for it.我正在尝试使用我的数据包管理器Home Brew安装 gtest,但没有它的存储库。 I tried to download gtest from code.google but I can't understand how to install it, because cmake and make don't solve the problem.我试图从 code.google 下载 gtest 但我无法理解如何安装它,因为cmakemake不能解决问题。

If you want the latest version without using Homebrew:如果您想要最新版本而不使用 Homebrew:

git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
make install

I just installed gtest using cmake and make.我刚刚使用 cmake 和 make 安装了 gtest。 I can show you how did I install Google Test manually.我可以向您展示我是如何手动安装 Google Test 的。 It's not complicated, just following the steps.不复杂,按照步骤来就行了。

  1. download the gtest file from https://github.com/google/googletest/archive/release-1.8.0.zip and unzip it.https://github.com/google/googletest/archive/release-1.8.0.zip下载 gtest 文件并解压。
cd googletest-release-1.8.0/googletest/
mkdir bld
cd bld
cmake -DCMAKE_CXX_FLAGS=-std=c++11 ..
make
cp -a ../include/gtest /usr/local/include
cp -a *.a /usr/local/lib
  1. delete the folder googletest-release-1.8.0 and release-1.8.0.zip删除文件夹googletest-release-1.8.0release-1.8.0.zip

If you want to use google-test please use #include <gtest/gtest.h> .如果您想使用 google-test,请使用#include <gtest/gtest.h>

If you want to use Google Test version 1.7.0, just download the release-1.7.0.zip and in the 2. step use cd googletest-release-1.7.0 instead of cd googletest-release-1.8.0/googletest/ .如果您想使用 Google Test 1.7.0 版,只需下载release-1.7.0.zip并在 2. 步骤中使用cd googletest-release-1.7.0而不是cd googletest-release-1.8.0/googletest/ . The rest steps are the same.其余步骤相同。

I added the flag -DCMAKE_CXX_FLAGS=-std=c++11 to use cmake with c++11 .我添加了标志-DCMAKE_CXX_FLAGS=-std=c++11以将cmakec++11一起使用。

Enjoy it!好好享受!


Updated version更新后的版本

Start from v1.8.0 gmock is included in the release file.从 v1.8.0 开始, gmock包含在发布文件中。

Here is the general steps for installing gtest and gmock in v1.8.0 - v1.10.0 .以下是在v1.8.0 - v1.10.0 中安装gtestgmock的一般步骤。 (Feel free to replace 1.10.0 with your desired version.) (随意将1.10.0替换为您想要的版本。)

# download release file and extract files from it
curl -LOk https://github.com/google/googletest/archive/release-1.10.0.tar.gz
tar -zxvf release-1.10.0.tar.gz
rm release-1.10.0.tar.gz

cd googletest-release-1.10.0/
mkdir build
cd build
# build gtest and gmock
cmake -DCMAKE_CXX_FLAGS=-std=c++11 ..
make

# copy requisite c++ files and compiled files to correct directories
cp -a ../googletest/include/* /usr/local/include
cp -a ../googlemock/include/* /usr/local/include
find . -name "*.a" -exec cp -a {} /usr/local/lib \;

# clean
cd ../..
rm -r googletest-release-1.10.0

If you want to install only gtest not gmock , you can do the following:如果您只想安装gtest而不是gmock ,您可以执行以下操作:

  1. remove the command cp -a ../googlemock/include/ /usr/local/include删除命令cp -a ../googlemock/include/ /usr/local/include

  2. replace代替

     find . -name "*.a" -exec cp -a {} /usr/local/lib \\;

    with

     find . -name "libgtest*.a" -exec cp -a {} /usr/local/lib \\;`

For the question 'Why there is no repository for it?'对于问题“为什么没有存储库?” see related gtest FAQ question .请参阅相关的 gtest 常见问题解答 But you can create formula by yourself if you want - see this post for the details (but don't sure if it will work for 1.6).但是,如果您愿意,您可以自己创建公式 - 有关详细信息,请参阅此帖子(但不确定它是否适用于 1.6)。

But I suggest you just install gtest: read the readme for the detailed instructions.但我建议您只安装 gtest:阅读自述文件以获取详细说明。 There are few simple steps:有几个简单的步骤:
Download and extract sources to some directory gtest_dir .下载源代码并将其解压缩到某个目录gtest_dir
Build object files:构建目标文件:

g++ -I$gtest_dir/include -I$gtest_dir -c $gtest_dir/src/gtest-all.cc
g++ -I$gtest_dir/include -I$gtest_dir -c $gtest_dir/src/gtest_main.cc

Link:关联:

ar -rv libgtest.a gtest-all.o
ar -rv libgtest_main.a gtest_main.o


Note : if you want to build gtest with support of C++11 and libc++ you need to do some extra work:注意:如果你想构建支持 C++11 和 libc++ 的 gtest,你需要做一些额外的工作:

你可以使用这个:

brew install --HEAD https://gist.githubusercontent.com/Kronuz/96ac10fbd8472eb1e7566d740c4034f8/raw/gtest.rb

This is an alternative solution for slund 's answer.这是slund答案的替代解决方案。 cmake .. didn't work for me. cmake ..对我不起作用。

cd desktop
git clone https://github.com/google/googletest.git
cd googletest
mkdir build
cd build
cmake -DCMAKE_CXX_COMPILER="c++" -DCMAKE_CXX_FLAGS="-std=c++11 -stdlib=libc++" ../
make
sudo make install

Latest Homebrew does no longer support installation from GitHub Gists, so @Kronuz's solution no longer works.最新的 Homebrew 不再支持从 GitHub Gists 安装,因此 @Kronuz 的解决方案不再有效。 Here's the error:这是错误:

Error: Calling Non-checksummed download of gtest formula file from an arbitrary URL is disabled!错误:从任意 URL 调用非校验和下载 gtest 公式文件被禁用! Use 'brew extract' or 'brew create' and 'brew tap-new' to create a formula file in a tap on GitHub instead.改为使用“brew extract”或“brew create”和“brew tap-new”在GitHub上的点击中创建公式文件。

GoogleTest can now be installed with the following command:现在可以使用以下命令安装 GoogleTest:

brew install web-eid/gtest/gtest

The underlying repository is based on @Kronuz's gist, thanks @Kronuz's for creating the gist!底层存储库基于@Kronuz 的要点,感谢@Kronuz 创建要点!

The ROS repo ( http://wiki.ros.org/kinetic/Installation/OSX/Homebrew/Source ) has gtest: ROS 存储库( http://wiki.ros.org/kinetic/Installation/OSX/Homebrew/Source )有 gtest:

brew tap ros/deps
brew instal gtest

Homebrew 公式: brew install googletest

If you want to install it from sources the simplest way is as follows如果您想从源代码安装它,最简单的方法如下

git clone https://github.com/google/googletest
cd googletest
cmake . -DCMAKE_BUILD_TYPE=Release 
make install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM