简体   繁体   中英

Cant compile googletest on OSX Yosemite

I have read a few related answers and some articles online as well as the official Google Test documentation, but since I lack experience in this area I am very insecure about a few things and seek guidance.

I am starting a brand new pet project in c++ and I want to write proper tests as I code along, ran into Google Test and decided to give it a try.

Currently this is my folder structure:

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

My idea is to have the tests and class files in their own folders, for example:

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

Question #1

Is there a naming convention or expectation for the file containing the tests? ( /lib/db/db.test in that example above)

Question #2

I ran the following commands

  1. cd lib/external/googletest/googletest
  2. cmake .
  3. make
  4. make install
  5. cp lib*.a /usr/local/lib
  6. cp -r include/gtest/ /usr/local/include/

And seeing how it ended up creating a series of .h files in /usr/local/include/gtest, I expected to be able to #include <gtest/gtest.h> and be able to run tests, I added this to my main.cpp:

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

and when building the following error showed up:

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

Solution

Thanks to trojanfoe I fixed my makefile to look like this:

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

And now it works just fine, please keep in mind that this is a skeleton project and I need to structure the test files and entry point, so at this point its just compiling a basic test.

您忘记将 google 测试库与您的可执行文件链接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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