我正在用 C++ 开发一个项目并编写单元测试并使用 Google 测试库。我使用的是带有 M1 芯片的 MacBook Pro。我通过自制软件安装了 Google Test。在自制的 GTest 页面上,提到 Gtest 与 Apple Silicon 兼容。
Apple Silicon Big Sur ✅
我正在使用 CLion。
这是我的 CMakeLists.txt 的样子:
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)
我需要 .dylib
才能在 arm64 上工作吗?不久前我试图链接 libpq 静态库,它给了我相同的链接错误。然后我尝试链接 .dylib 而不是 .a 并且它在 arm64 上工作。但是我在 lib 目录中没有看到 GTest 的动态库。
以下是通过 homebrew 安装 GTest 后的所有库:
我得到的链接错误是:
[ 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
这是我为测试 Google 测试而编写的简单代码:
#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();
}