I am developing a project in C++ and writing unit tests and using Google Test library for the same. I am on a MacBook Pro with M1 Chip. I installed Google Test through homebrew. On homebrew GTest page, it's mentioned that Gtest is compatible with Apple Silicon.
Apple Silicon Big Sur ✅
I am using CLion.
Here's what my CMakeLists.txt looks like:
cmake_minimum_required(VERSION 3.19)
project(GTest)
include_directories(/opt/homebrew/Cellar/googletest/1.10.0/include)
link_directories(/opt/homebrew/Cellar/googletest/1.10.0/lib)
set(CMAKE_CXX_STANDARD 14)
add_executable(GTest main.cpp)
target_link_libraries(GTest libgmock_main.a)
Do I need a .dylib for this to work on arm64? I was trying to link the libpq static library a while back and it gave me the same linking error. Then I tried to link .dylib instead of .a and it worked on arm64. But I don't see a dynamic library for GTest in lib directory.
Here are all the libraries that are there after installing GTest through homebrew:
- libgmock_main.a
- libgtest_main.a
- libgmock.a
- libgtest.a
The linking error I am getting is:
[ 50%] Linking CXX executable GTest
Undefined symbols for architecture arm64:
"testing::InitGoogleMock(int*, char**)", referenced from:
_main in libgmock_main.a(gmock_main.cc.o)
"testing::Test::SetUp()", referenced from:
vtable for TestGetStr_First_Test in main.cpp.o
"testing::Test::TearDown()", referenced from:
vtable for TestGetStr_First_Test in main.cpp.o
"testing::Test::Test()", referenced from:
TestGetStr_First_Test::TestGetStr_First_Test() in main.cpp.o
"testing::Test::~Test()", referenced from:
TestGetStr_First_Test::~TestGetStr_First_Test() in main.cpp.o
"testing::Message::Message()", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::UnitTest::GetInstance()", referenced from:
_main in libgmock_main.a(gmock_main.cc.o)
"testing::UnitTest::Run()", referenced from:
_main in libgmock_main.a(gmock_main.cc.o)
"testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::internal::AssertHelper::~AssertHelper()", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::internal::GetTestTypeId()", referenced from:
___cxx_global_var_init in main.cpp.o
"testing::internal::CmpHelperSTREQ(char const*, char const*, char const*, char const*)", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.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.cpp.o
"testing::internal::IsTrue(bool)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in main.cpp.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in main.cpp.o
"testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in main.cpp.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in main.cpp.o
"testing::internal::GTestLog::~GTestLog()", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in main.cpp.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in main.cpp.o
"testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"typeinfo for testing::Test", referenced from:
typeinfo for TestGetStr_First_Test in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [GTest] Error 1
make[2]: *** [CMakeFiles/GTest.dir/all] Error 2
make[1]: *** [CMakeFiles/GTest.dir/rule] Error 2
make: *** [GTest] Error 2
Here's the simple code that I wrote to test Google Test:
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
string getStr(string str)
{
return "1";
}
TEST(TestGetStr, First)
{
ASSERT_STREQ("2", getStr("Arun").c_str());
}
int main()
{
::testing::InitGoogleTest();
return RUN_ALL_TESTS();
}