如何在 ubuntu 上使用 make 修复主建筑 gtest 项目的多个定义
How to fix multiple definition of main building gtest project using make on ubuntu
我想在 linux 上构建一个 gtest 项目,只使用一个 makefile,即不使用 cmake。我按照这里的教程进行操作:
http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html
但是得到主要错误的多个定义 - 见下文。
我在 ubuntu v20.04
angus@angus-VirtualBox:~/Documents/code/gtest_make_only/test/src$ uname -a
Linux angus-VirtualBox 5.11.0-38-generic #42~20.04.1-Ubuntu SMP 9 月 28 日星期二 20:41:07 UTC 2021 x86_64 x86_64 x86_64GNU/Linux
我是这样安装 gtest 的:
git clone https://github.com/google/googletest.git
mkdir build
cd build
sudo snap install cmake --classic
cmake .. -DBUILD_GMOCK=OFF
make
sudo make install
如果我现在查看我的系统,我有
/usr/local/include - 带头文件的 gtest 目录
/usr/local/lib - 包含 libgtest.a 和 libgtest_main.a
我把例子中的Makefile修改成这样:
CXX = g++
CXXFLAGS = -g -L/usr/local/lib -lgtest -lgtest_main -lpthread
INCS = -I./ -I../../src -I/usr/local/include
OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
testAll: $(OBJS)
$(CXX) $(CXXFLAGS) $(INCS) -o testAll Main_TestAll.cpp $(OBJS)
.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS)
clean:
rm testAll *.o testAll.xml
我还必须整理代码示例 - 例如代码中有 Addition.h 的地方 - 必须更改为文件名 Addition.hpp。
下面的构建错误。如何解决这个问题?
我确实尝试从 Makefile 中删除 -lgtest_main,但随后出现错误:
/home/angus/Documents/code/gtest_make_only/test/src/Main_TestAll.cpp:5: undefined reference to `testing::InitGoogleTest(int*, char**)'
构建错误:
angus@angus-VirtualBox:~/Documents/code/gtest_make_only/test/src$ make
g++ -g -L/usr/local/lib -lgtest -lgtest_main -lpthread -c Multiply_Test.cpp -o Multiply_Test.o -I./ -I../../src -I/usr/local/include
g++ -g -L/usr/local/lib -lgtest -lgtest_main -lpthread -I./ -I../../src -I/usr/local/include -o testAll Main_TestAll.cpp ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
/usr/bin/ld: /tmp/cc4xkhF1.o: in function `RUN_ALL_TESTS()':
/home/angus/Documents/code/gtest_make_only/test/src/Main_TestAll.cpp:4: multiple definition of `main'; /usr/local/lib/libgtest_main.a(gtest_main.cc.o):gtest_main.cc:(.text+0x0): first defined here
/usr/bin/ld: /usr/local/lib/libgtest_main.a(gtest_main.cc.o): in function `main':
gtest_main.cc:(.text+0x3a): undefined reference to `testing::InitGoogleTest(int*, char**)'
/usr/bin/ld: /usr/local/lib/libgtest_main.a(gtest_main.cc.o): in function `RUN_ALL_TESTS()':
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x9): undefined reference to `testing::UnitTest::GetInstance()'
/usr/bin/ld: gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x11): undefined reference to `testing::UnitTest::Run()'
/usr/bin/ld: /tmp/cc4xkhF1.o: in function `main':
/home/angus/Documents/code/gtest_make_only/test/src/Main_TestAll.cpp:5: undefined reference to `testing::InitGoogleTest(int*, char**)'
/usr/bin/ld: Addition_Test.o: in function `AdditionTest_twoValues_Test::TestBody()':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: Addition_Test.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:16: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/usr/bin/ld: Addition_Test.o: in function `testing::internal::SuiteApiResolver<AdditionTest>::GetSetUpCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Addition_Test.o: in function `testing::internal::SuiteApiResolver<AdditionTest>::GetTearDownCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Addition_Test.o: in function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1549: undefined reference to `testing::AssertionSuccess()'
/usr/bin/ld: Addition_Test.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1532: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: Addition_Test.o: in function `AdditionTest::~AdditionTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:5: undefined reference to `testing::Test::~Test()'
/usr/bin/ld: Addition_Test.o:(.data.rel.ro._ZTI12AdditionTest[_ZTI12AdditionTest]+0x10): undefined reference to `typeinfo for testing::Test'
/usr/bin/ld: Addition_Test.o: in function `AdditionTest::AdditionTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:5: undefined reference to `testing::Test::Test()'
/usr/bin/ld: Multiply_Test.o: in function `MultiplyTest_twoValues_Test::TestBody()':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: Multiply_Test.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:14: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/usr/bin/ld: Multiply_Test.o: in function `testing::internal::SuiteApiResolver<MultiplyTest>::GetSetUpCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Multiply_Test.o: in function `testing::internal::SuiteApiResolver<MultiplyTest>::GetTearDownCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Multiply_Test.o: in function `MultiplyTest::~MultiplyTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:5: undefined reference to `testing::Test::~Test()'
/usr/bin/ld: Multiply_Test.o:(.data.rel.ro._ZTI12MultiplyTest[_ZTI12MultiplyTest]+0x10): undefined reference to `typeinfo for testing::Test'
/usr/bin/ld: Multiply_Test.o: in function `MultiplyTest::MultiplyTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:5: undefined reference to `testing::Test::Test()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:7: testAll] Error 1
你的命令行全错了。
库规范应遵循 源文件和目标文件。正确的行应如下所示:
g++ -g -I./ -I../../src -o testAll Main_TestAll.cpp ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o -lgtest_main -lgtest -lpthread
请参阅 this blog post 了解说明。
我想在 linux 上构建一个 gtest 项目,只使用一个 makefile,即不使用 cmake。我按照这里的教程进行操作:
http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html
但是得到主要错误的多个定义 - 见下文。
我在 ubuntu v20.04
angus@angus-VirtualBox:~/Documents/code/gtest_make_only/test/src$ uname -a Linux angus-VirtualBox 5.11.0-38-generic #42~20.04.1-Ubuntu SMP 9 月 28 日星期二 20:41:07 UTC 2021 x86_64 x86_64 x86_64GNU/Linux
我是这样安装 gtest 的:
git clone https://github.com/google/googletest.git
mkdir build
cd build
sudo snap install cmake --classic
cmake .. -DBUILD_GMOCK=OFF
make
sudo make install
如果我现在查看我的系统,我有
/usr/local/include - 带头文件的 gtest 目录
/usr/local/lib - 包含 libgtest.a 和 libgtest_main.a
我把例子中的Makefile修改成这样:
CXX = g++
CXXFLAGS = -g -L/usr/local/lib -lgtest -lgtest_main -lpthread
INCS = -I./ -I../../src -I/usr/local/include
OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
testAll: $(OBJS)
$(CXX) $(CXXFLAGS) $(INCS) -o testAll Main_TestAll.cpp $(OBJS)
.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS)
clean:
rm testAll *.o testAll.xml
我还必须整理代码示例 - 例如代码中有 Addition.h 的地方 - 必须更改为文件名 Addition.hpp。
下面的构建错误。如何解决这个问题?
我确实尝试从 Makefile 中删除 -lgtest_main,但随后出现错误:
/home/angus/Documents/code/gtest_make_only/test/src/Main_TestAll.cpp:5: undefined reference to `testing::InitGoogleTest(int*, char**)'
构建错误:
angus@angus-VirtualBox:~/Documents/code/gtest_make_only/test/src$ make
g++ -g -L/usr/local/lib -lgtest -lgtest_main -lpthread -c Multiply_Test.cpp -o Multiply_Test.o -I./ -I../../src -I/usr/local/include
g++ -g -L/usr/local/lib -lgtest -lgtest_main -lpthread -I./ -I../../src -I/usr/local/include -o testAll Main_TestAll.cpp ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
/usr/bin/ld: /tmp/cc4xkhF1.o: in function `RUN_ALL_TESTS()':
/home/angus/Documents/code/gtest_make_only/test/src/Main_TestAll.cpp:4: multiple definition of `main'; /usr/local/lib/libgtest_main.a(gtest_main.cc.o):gtest_main.cc:(.text+0x0): first defined here
/usr/bin/ld: /usr/local/lib/libgtest_main.a(gtest_main.cc.o): in function `main':
gtest_main.cc:(.text+0x3a): undefined reference to `testing::InitGoogleTest(int*, char**)'
/usr/bin/ld: /usr/local/lib/libgtest_main.a(gtest_main.cc.o): in function `RUN_ALL_TESTS()':
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x9): undefined reference to `testing::UnitTest::GetInstance()'
/usr/bin/ld: gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x11): undefined reference to `testing::UnitTest::Run()'
/usr/bin/ld: /tmp/cc4xkhF1.o: in function `main':
/home/angus/Documents/code/gtest_make_only/test/src/Main_TestAll.cpp:5: undefined reference to `testing::InitGoogleTest(int*, char**)'
/usr/bin/ld: Addition_Test.o: in function `AdditionTest_twoValues_Test::TestBody()':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:20: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:21: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: Addition_Test.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:16: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/usr/bin/ld: Addition_Test.o: in function `testing::internal::SuiteApiResolver<AdditionTest>::GetSetUpCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Addition_Test.o: in function `testing::internal::SuiteApiResolver<AdditionTest>::GetTearDownCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Addition_Test.o: in function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1549: undefined reference to `testing::AssertionSuccess()'
/usr/bin/ld: Addition_Test.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1532: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: Addition_Test.o: in function `AdditionTest::~AdditionTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:5: undefined reference to `testing::Test::~Test()'
/usr/bin/ld: Addition_Test.o:(.data.rel.ro._ZTI12AdditionTest[_ZTI12AdditionTest]+0x10): undefined reference to `typeinfo for testing::Test'
/usr/bin/ld: Addition_Test.o: in function `AdditionTest::AdditionTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Addition_Test.cpp:5: undefined reference to `testing::Test::Test()'
/usr/bin/ld: Multiply_Test.o: in function `MultiplyTest_twoValues_Test::TestBody()':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:18: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:19: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: Multiply_Test.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:14: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/usr/bin/ld: Multiply_Test.o: in function `testing::internal::SuiteApiResolver<MultiplyTest>::GetSetUpCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:527: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Multiply_Test.o: in function `testing::internal::SuiteApiResolver<MultiplyTest>::GetTearDownCaseOrSuite(char const*, int)':
/usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /usr/local/include/gtest/internal/gtest-internal.h:548: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: Multiply_Test.o: in function `MultiplyTest::~MultiplyTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:5: undefined reference to `testing::Test::~Test()'
/usr/bin/ld: Multiply_Test.o:(.data.rel.ro._ZTI12MultiplyTest[_ZTI12MultiplyTest]+0x10): undefined reference to `typeinfo for testing::Test'
/usr/bin/ld: Multiply_Test.o: in function `MultiplyTest::MultiplyTest()':
/home/angus/Documents/code/gtest_make_only/test/src/Multiply_Test.cpp:5: undefined reference to `testing::Test::Test()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:7: testAll] Error 1
你的命令行全错了。
库规范应遵循 源文件和目标文件。正确的行应如下所示:
g++ -g -I./ -I../../src -o testAll Main_TestAll.cpp ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o -lgtest_main -lgtest -lpthread
请参阅 this blog post 了解说明。