在 Visual Studio 2022 中将 google 测试构建为 DLL 链接错误

问题描述 投票:0回答:1

我刚刚克隆了 https://github.com/google/googletest,创建了一个新的 Visual Studio(2022 专业版)DLL 项目并复制了目录 https://github.com/google/googletest/tree/main/googletest /include/https://github.com/google/googletest/tree/main/googletest/src 到项目文件夹中,并将

$(ProjectDir);$(ProjectDir)include
添加到项目的“属性 --> C++ --> 其他包含目录” " 然后将所有
#include "gtest/
替换为
#include "../include/gtest/
,将所有
#include "src/
替换为
#include "../src/
并构建项目。编译很顺利,但链接却没有,因为我有大约 400 个链接错误:

1>gtest-assertion-result.obj : error LNK2005: "public: __cdecl testing::AssertionResult::AssertionResult(class testing::AssertionResult const &)" (??0AssertionResult@testing@@QEAA@AEBV01@@Z) already defined in gtest-all.obj
1>gtest-assertion-result.obj : error LNK2005: "public: class testing::AssertionResult __cdecl testing::AssertionResult::operator!(void)const " (??7AssertionResult@testing@@QEBA?AV01@XZ) already defined in gtest-all.obj
1>gtest-assertion-result.obj : error LNK2005: "class testing::AssertionResult __cdecl testing::AssertionFailure(class testing::Message const &)" (?AssertionFailure@testing@@YA?AVAssertionResult@1@AEBVMessage@1@@Z) already defined in gtest-all.obj
1>gtest-assertion-result.obj : error LNK2005: "class testing::AssertionResult __cdecl testing::AssertionFailure(void)" (?AssertionFailure@testing@@YA?AVAssertionResult@1@XZ) already defined in gtest-all.obj
1>gtest-assertion-result.obj : error LNK2005: "class testing::AssertionResult __cdecl testing::AssertionSuccess(void)" (?AssertionSuccess@testing@@YA?AVAssertionResult@1@XZ) already defined in gtest-all.obj
1>gtest-assertion-result.obj : error LNK2005: "private: void __cdecl testing::AssertionResult::swap(class testing::AssertionResult &)" (?swap@AssertionResult@testing@@AEAAXAEAV12@@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "public: __cdecl testing::internal::DeathTest::DeathTest(void)" (??0DeathTest@internal@testing@@QEAA@XZ) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "public: __cdecl testing::ExitedWithCode::ExitedWithCode(int)" (??0ExitedWithCode@testing@@QEAA@H@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "public: bool __cdecl testing::ExitedWithCode::operator()(int)const " (??RExitedWithCode@testing@@QEBA_NH@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "protected: virtual void __cdecl testing::internal::DeathTestImpl::Abort(enum testing::internal::DeathTest::AbortReason)" (?Abort@DeathTestImpl@internal@testing@@MEAAXW4AbortReason@DeathTest@23@@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "public: virtual enum testing::internal::DeathTest::TestRole __cdecl testing::internal::WindowsDeathTest::AssumeRole(void)" (?AssumeRole@WindowsDeathTest@internal@testing@@UEAA?AW4TestRole@DeathTest@23@XZ) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "public: static bool __cdecl testing::internal::DeathTest::Create(char const *,class testing::Matcher<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &>,char const *,int,class testing::internal::DeathTest * *)" (?Create@DeathTest@internal@testing@@SA_NPEBDV?$Matcher@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@3@0HPEAPEAV123@@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "public: virtual bool __cdecl testing::internal::DefaultDeathTestFactory::Create(char const *,class testing::Matcher<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &>,char const *,int,class testing::internal::DeathTest * *)" (?Create@DefaultDeathTestFactory@internal@testing@@UEAA_NPEBDV?$Matcher@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@3@0HPEAPEAVDeathTest@23@@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "bool __cdecl testing::internal::ExitedUnsuccessfully(int)" (?ExitedUnsuccessfully@internal@testing@@YA_NH@Z) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "protected: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl testing::internal::DeathTestImpl::GetErrorLogs(void)" (?GetErrorLogs@DeathTestImpl@internal@testing@@MEAA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl testing::internal::GetLastErrnoDescription(void)" (?GetLastErrnoDescription@internal@testing@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in gtest-all.obj
1>gtest-death-test.obj : error LNK2005: "bool __cdecl testing::internal::InDeathTestChild(void)" (?InDeathTestChild@internal@testing@@YA_NXZ) already defined in gtest-all.obj
...
1>gtest.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > testing::FLAGS_gtest_stream_result_to" (?FLAGS_gtest_stream_result_to@testing@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in gtest-all.obj
1>gtest.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > testing::FLAGS_gtest_flagfile" (?FLAGS_gtest_flagfile@testing@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in gtest-all.obj
1>gtest.obj : error LNK2005: "void const * const testing::internal::kTestTypeIdInGoogleTest" (?kTestTypeIdInGoogleTest@internal@testing@@3QEBXEB) already defined in gtest-all.obj
1>C:\Googletest\x64\Debug\Googletest.dll : fatal error LNK1169: one or more multiply defined symbols found

我做错了什么?

c++ linker-errors googletest
1个回答
0
投票

查看 gtest-all.cc:

// The following lines pull in the real gtest *.cc files.
#include "src/gtest-assertion-result.cc"
#include "src/gtest-death-test.cc"
#include "src/gtest-filepath.cc"
#include "src/gtest-matchers.cc"
#include "src/gtest-port.cc"
#include "src/gtest-printers.cc"
#include "src/gtest-test-part.cc"
#include "src/gtest-typed-test.cc"
#include "src/gtest.cc"

包含代表您构建 gtest-all.obj 的所有其他源文件。

换句话说,您应该构建的唯一 .cc 文件是 gtest-all.cc。 否则,您将收到重复的符号错误。

从项目源文件列表中删除所有其他 .cc 文件。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.