Unable to compile my file #4129
-
Environment: This is my first time to use googletest. I successfully installed it and then try to run my first program: #include <gtest/gtest.h>
int test(int num) { return num + 1; }
TEST(simpleTest, one) { EXPECT_EQ(test(10), 11); }
int main() {
testing::InitGoogleTest();
return RUN_ALL_TESTS();
} I try to compile it using: clang++ -std=17 main.cpp -o main.out But my terminal just show me the folloing wierd things: Undefined symbols for architecture arm64:
"testing::InitGoogleTest()", referenced from:
_main in a-6ef9ee.o
"testing::AssertionSuccess()", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in a-6ef9ee.o
"testing::Test::SetUp()", referenced from:
vtable for simpleTest_one_Test in a-6ef9ee.o
"testing::Test::TearDown()", referenced from:
vtable for simpleTest_one_Test in a-6ef9ee.o
"testing::Test::Test()", referenced from:
simpleTest_one_Test::simpleTest_one_Test() in a-6ef9ee.o
"testing::Test::~Test()", referenced from:
simpleTest_one_Test::~simpleTest_one_Test() in a-6ef9ee.o
"testing::Message::Message()", referenced from:
simpleTest_one_Test::TestBody() in a-6ef9ee.o
"testing::UnitTest::GetInstance()", referenced from:
RUN_ALL_TESTS() in a-6ef9ee.o
"testing::UnitTest::Run()", referenced from:
RUN_ALL_TESTS() in a-6ef9ee.o
"testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
simpleTest_one_Test::TestBody() in a-6ef9ee.o
"testing::internal::AssertHelper::~AssertHelper()", referenced from:
simpleTest_one_Test::TestBody() in a-6ef9ee.o
"testing::internal::GetTestTypeId()", referenced from:
___cxx_global_var_init in a-6ef9ee.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 a-6ef9ee.o
"testing::internal::IsTrue(bool)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in a-6ef9ee.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in a-6ef9ee.o
"testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in a-6ef9ee.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in a-6ef9ee.o
"testing::internal::GTestLog::~GTestLog()", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in a-6ef9ee.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in a-6ef9ee.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 a-6ef9ee.o
"testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
simpleTest_one_Test::TestBody() in a-6ef9ee.o
"typeinfo for testing::Test", referenced from:
typeinfo for simpleTest_one_Test in a-6ef9ee.o
ld: symbol(s) not found for architecture arm64 What did I do wrong? Thank you!!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It seems like the linker is unable to find the symbols for the Google Test library. This is likely because the library is not being linked to your program during the compilation process. To fix this, you should link the Google Test library to your program by adding the following flag to your compile command: -lgtest It should look like this: clang++ -std=17 main.cpp -o main.out -lgtest This flag tells the linker to link the gtest library to your program. If the library is installed in a non-standard location, you may also need to use the -L flag to specify the location of the library and the -I flag to specify the location of the gtest header files. Also make sure that the version of the library you're linking to is compatible with your clang version. |
Beta Was this translation helpful? Give feedback.
It seems like the linker is unable to find the symbols for the Google Test library. This is likely because the library is not being linked to your program during the compilation process. To fix this, you should link the Google Test library to your program by adding the following flag to your compile command:
-lgtest
It should look like this:
clang++ -std=17 main.cpp -o main.out -lgtest
This flag tells the linker to link the gtest library to your program. If the library is installed in a non-standard location, you may also need to use the -L flag to specify the location of the library and the -I flag to specify the location of the gtest header files.
Also make sure that the version of the …