无法在 OSX Yosemite 上编译 googletest

Cant compile googletest on OSX Yosemite

网上看了一些相关的回答和一些文章,也看了官方的Google测试文档,但是由于缺乏这方面的经验,有些地方很不放心,求指教。

我正在用 c++ 开始一个 b运行d 新宠物项目,我想在编写代码时编写适当的测试,运行 进入 Google 测试并决定给它一个尝试。

目前这是我的文件夹结构:

/
/bin
/main.cpp
/lib
/lib/db
/lib/external/googletest

我的想法是将测试和 class 文件放在它们自己的文件夹中,例如:

/lib/db/db.h
/lib/db/db.cpp
/lib/db/db.test

问题 #1

包含测试的文件是否有命名约定或期望? (/lib/db/db.test 在上面的例子中)

问题 #2

我运行以下命令

  1. cd lib/external/googletest/googletest
  2. cmake .
  3. 制作
  4. 进行安装
  5. cp 库*.a /usr/local/lib
  6. cp -r include/gtest//usr/local/include/

看到它最终如何在 /usr/local/include/gtest 中创建一系列 .h 文件,我希望能够 #include <gtest/gtest.h> 并且能够 运行测试,我将其添加到我的 main.cpp:

TEST(Param1, Param2) {
    EXPECT_EQ(1, 1);
}

构建时出现以下错误:

Undefined symbols for architecture x86_64:
  "testing::AssertionSuccess()", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in main-230b96.o
  "testing::Test::SetUp()", referenced from:
      vtable for Param1_Param2_Test in main-230b96.o
  "testing::Test::TearDown()", referenced from:
      vtable for Param1_Param2_Test in main-230b96.o
  "testing::Test::Test()", referenced from:
      Param1_Param2_Test::Param1_Param2_Test() in main-230b96.o
  "testing::Test::~Test()", referenced from:
      Param1_Param2_Test::~Param1_Param2_Test() in main-230b96.o
  "testing::Message::Message()", referenced from:
      Param1_Param2_Test::TestBody() in main-230b96.o
  "testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
      Param1_Param2_Test::TestBody() in main-230b96.o
  "testing::internal::AssertHelper::~AssertHelper()", referenced from:
      Param1_Param2_Test::TestBody() in main-230b96.o
  "testing::internal::GetTestTypeId()", referenced from:
      ___cxx_global_var_init in main-230b96.o
  "testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
      ___cxx_global_var_init in main-230b96.o
  "testing::internal::IsTrue(bool)", referenced from:
      testing::internal::scoped_ptr<std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::reset(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in main-230b96.o
      testing::internal::scoped_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::reset(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in main-230b96.o
  "testing::internal::EqFailure(char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&) in main-230b96.o
  "testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
      Param1_Param2_Test::TestBody() in main-230b96.o
  "typeinfo for testing::Test", referenced from:
      typeinfo for Param1_Param2_Test in main-230b96.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

解决方案

感谢 trojanfoe,我将 makefile 修改为如下所示:

CC=g++
target_folder=bin
all:
    $(CC) *.cpp -o $(target_folder)/prod
    ./bin/prod
test:
    $(CC) *.cpp -o $(target_folder)/test lib/external/googletest/googletest/libgtest.a lib/external/googletest/googletest/libgtest_main.a
    ./bin/test
clean:
    rm -f $(target_folder)/* *.o

现在它工作得很好,请记住这是一个骨架项目,我需要构建测试文件和入口点,所以此时它只是编译一个基本测试。

您忘记了 link-in 带有可执行文件的 google 测试库。