找不到 gtest.h 文件 googletest xcode 7.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32797023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:56:39  来源:igfitidea点击:

gtest.h file not found googletest xcode 7.0

c++xcodemacosunit-testinggoogletest

提问by Allan Ojala

I'm trying to setup google testing framework for my c++ project following this guidein xcode 7.0 I got to the last step Build and Go but after hours of searching online I can't get my test project to run. The compiler does not seem to find the headers it needs. main.cpp:9:10: 'gtest/gtest.h' file not found. The source is:

我正在尝试按照 xcode 7.0 中的本指南为我的 c++ 项目设置谷歌测试框架我到了最后一步 Build and Go 但是在网上搜索了几个小时后我无法运行我的测试项目。编译器似乎没有找到它需要的头文件。main.cpp:9:10: 找不到“gtest/gtest.h”文件。来源是:

#include "gtest/gtest.h"

#include "calc.hpp"

int main(int argc, const char * argv[]) {

    return 0;
}

I also tried #include <gtest/gtest.h> With same result.

我也试过 #include <gtest/gtest.h> 具有相同的结果。

回答by Allan Ojala

Here is how I got it to work:

这是我让它工作的方式:

Steps:

脚步:

  1. Download the source code $ svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only

  2. cdto the folder 'make' in the downloaded source code folder

  3. run $ make gtest.a gtest_main.a
  4. manually copy the two static library files to /usr/local/lib and copy the header file dir 'include/gtest' to /usr/local/include.
  5. Add Header Search Path (/usr/local/include) and Library Search Path (/usr/local/lib) to your xcode's project setting
  6. Add linker flag agains gtest. In target setting under "Other Linker Flags". Add /usr/local/lib/gtest.a

    // main.cpp
    #include <stdio.h>
    #include "gtest/gtest.h"
    #include "calc.hpp" // has function int addition(int,int);
    TEST(MyFirstTest, TestAddition) {
            EXPECT_EQ(3, addition(1, 2));
    }
    GTEST_API_ int main(int argc, char **argv) {
            printf("Running main() from gtest_main.cc\n");
            testing::InitGoogleTest(&argc, argv);
            return RUN_ALL_TESTS();
    }
    
  1. 下载源代码 $ svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only

  2. cd到下载的源代码文件夹中的文件夹“make”

  3. $ make gtest.a gtest_main.a
  4. 手动将两个静态库文件复制到/usr/local/lib,将头文件目录'include/gtest'复制到/usr/local/include。
  5. 将标题搜索路径 (/usr/local/include) 和库搜索路径 (/usr/local/lib) 添加到您的 xcode 项目设置
  6. 在 gtest 中添加链接器标志。在“其他链接器标志”下的目标设置中。添加 /usr/local/lib/gtest.a

    // main.cpp
    #include <stdio.h>
    #include "gtest/gtest.h"
    #include "calc.hpp" // has function int addition(int,int);
    TEST(MyFirstTest, TestAddition) {
            EXPECT_EQ(3, addition(1, 2));
    }
    GTEST_API_ int main(int argc, char **argv) {
            printf("Running main() from gtest_main.cc\n");
            testing::InitGoogleTest(&argc, argv);
            return RUN_ALL_TESTS();
    }
    

回答by Zhen Zhou

Sometimes, Xcode cannot find header files in framework. You need additional steps to make it work.

有时,Xcode 在框架中找不到头文件。您需要额外的步骤才能使其工作。

  • In build settings, complete Framework Search Pathswith the path to your framework, which is gtest.framework.

  • Add the framework path and to User Header Search Paths.

  • If Xcode cannot find "gtest/internal/gtest-port-arch.h", you can find it in source folder "googletest/googletest/include". Add it to User Header Search Paths.

  • 构建设置中,使用您的框架路径完成框架搜索路径,即 gtest.framework。

  • 将框架路径和添加到User Header Search Paths

  • 如果 Xcode 找不到“gtest/internal/gtest-port-arch.h”,您可以在源文件夹“googletest/googletest/include”中找到它。将其添加到User Header Search Paths

After these steps, gtest should work in Xcode 7.0.

完成这些步骤后,gtest 应该可以在 Xcode 7.0 中运行了。

回答by dangerousdave

Try deleting the contents of your DerrivedData folder, and re-building.

尝试删除 DerivedData 文件夹的内容,然后重新构建。

enter image description here

在此处输入图片说明