在 Mac 上构建 Google 测试时出现链接错误(命令行)

发布于 2025-01-03 00:12:29 字数 4842 浏览 0 评论 0原文

我目前正在尝试构建一些使用 Google C++ 测试框架的测试代码,但我不断收到错误消息

ld: warning: in /usr/local/lib/libgtest.dylib, file wasbuilt for unsupported file format which is not the Architecture正在链接(i386)

我试图使问题尽可能简单:

我有一个主要功能 cmtest.cc

#include <gtest/gtest.h>

/** Main entry point */
int main(int argc, char**argv, char**envArg)
{
    testing::InitGoogleTest(&argc, argv);
    return(RUN_ALL_TESTS());
}

真正的基本测试代码 CrazyTest.cc

#include <gtest/gtest.h>
TEST(CrazyTest, one) {
    EXPECT_EQ(2, 2);
}

我使用以下命令来构建 gtest 和我的测试代码。

g++ -o CrazyTest.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0/include CrazyTest.cc

g++ -o cmtest.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0/include cmtest.cc

g++ -o gtest-all.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0 -I/opt/gtest-1.6.0/include /opt/gtest-1.6.0/src/gtest-all.cc

ar rc libgtest.a gtest-all.o

ranlib libgtest.a

g++ -o cmtest -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 CrazyTest.o cmtest.o -lstdc++ -lgtest

最后的构建步骤给了我以下错误,我无法找出原因。我能够获得实际测试(不是所示的简单测试),以在 mac OS(leopard)给我带来问题的其他操作系统上构建。

 ld: warning: in /usr/local/lib/libgtest.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols for architecture i386:
  "testing::Test::~Test()", referenced from:
      CrazyTest_one_Test::~CrazyTest_one_Test()in CrazyTest.o
      CrazyTest_one_Test::~CrazyTest_one_Test()in CrazyTest.o
  "testing::internal::AssertHelper::~AssertHelper()", referenced from:
      CrazyTest_one_Test::TestBody()      in CrazyTest.o
  "testing::Test::Test()", referenced from:
      CrazyTest_one_Test::CrazyTest_one_Test()in CrazyTest.o
  "testing::internal::GetTestTypeId()", referenced from:
      __static_initialization_and_destruction_0(int, int)in CrazyTest.o
  "testing::Test::TearDown()", referenced from:
      vtable for CrazyTest_one_Testin CrazyTest.o
  "testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)in CrazyTest.o
  "testing::UnitTest::GetInstance()", referenced from:
      _main in cmtest.o
  "testing::internal::IsTrue(bool)", referenced from:
      testing::internal::scoped_ptr<std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)in CrazyTest.o
      testing::internal::scoped_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)in CrazyTest.o
  "testing::UnitTest::Run()", referenced from:
      _main in cmtest.o
  "testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
      CrazyTest_one_Test::TestBody()      in CrazyTest.o
  "testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
      CrazyTest_one_Test::TestBody()      in CrazyTest.o
  "testing::AssertionSuccess()", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)in CrazyTest.o
  "testing::Test::SetUp()", referenced from:
      vtable for CrazyTest_one_Testin CrazyTest.o
  "testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
      __static_initialization_and_destruction_0(int, int)in CrazyTest.o
  "testing::InitGoogleTest(int*, char**)", referenced from:
      _main in cmtest.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
lipo: can't open input file: /var/tmp//ccZQiF8k.out (No such file or directory)

我已经为我构建的所有内容定义了 -arch i386 和 -arch x86_64 ,所以我无法弄清楚我错过了什么。我很少在 Mac 上进行编程,这个特殊问题让我陷入困境。

任何建议都会有帮助。

I am currently trying to build some test code that uses Google C++ Test framework but I keep getting an error stating

ld: warning: in /usr/local/lib/libgtest.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

I have tried to make the issue as simple as possible:

I have a main function
cmtest.cc

#include <gtest/gtest.h>

/** Main entry point */
int main(int argc, char**argv, char**envArg)
{
    testing::InitGoogleTest(&argc, argv);
    return(RUN_ALL_TESTS());
}

really basic test code
CrazyTest.cc

#include <gtest/gtest.h>
TEST(CrazyTest, one) {
    EXPECT_EQ(2, 2);
}

I use the following commands to build gtest and my test code.

g++ -o CrazyTest.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0/include CrazyTest.cc

g++ -o cmtest.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0/include cmtest.cc

g++ -o gtest-all.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0 -I/opt/gtest-1.6.0/include /opt/gtest-1.6.0/src/gtest-all.cc

ar rc libgtest.a gtest-all.o

ranlib libgtest.a

g++ -o cmtest -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 CrazyTest.o cmtest.o -lstdc++ -lgtest

The final build step gives me the following error and I am unable to figure out why. I am able to get the actual tests (not the simple one shown) to build on other OSs the mac OS (leopard) is giving me problems.

 ld: warning: in /usr/local/lib/libgtest.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols for architecture i386:
  "testing::Test::~Test()", referenced from:
      CrazyTest_one_Test::~CrazyTest_one_Test()in CrazyTest.o
      CrazyTest_one_Test::~CrazyTest_one_Test()in CrazyTest.o
  "testing::internal::AssertHelper::~AssertHelper()", referenced from:
      CrazyTest_one_Test::TestBody()      in CrazyTest.o
  "testing::Test::Test()", referenced from:
      CrazyTest_one_Test::CrazyTest_one_Test()in CrazyTest.o
  "testing::internal::GetTestTypeId()", referenced from:
      __static_initialization_and_destruction_0(int, int)in CrazyTest.o
  "testing::Test::TearDown()", referenced from:
      vtable for CrazyTest_one_Testin CrazyTest.o
  "testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)in CrazyTest.o
  "testing::UnitTest::GetInstance()", referenced from:
      _main in cmtest.o
  "testing::internal::IsTrue(bool)", referenced from:
      testing::internal::scoped_ptr<std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)in CrazyTest.o
      testing::internal::scoped_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)in CrazyTest.o
  "testing::UnitTest::Run()", referenced from:
      _main in cmtest.o
  "testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
      CrazyTest_one_Test::TestBody()      in CrazyTest.o
  "testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
      CrazyTest_one_Test::TestBody()      in CrazyTest.o
  "testing::AssertionSuccess()", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)in CrazyTest.o
  "testing::Test::SetUp()", referenced from:
      vtable for CrazyTest_one_Testin CrazyTest.o
  "testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
      __static_initialization_and_destruction_0(int, int)in CrazyTest.o
  "testing::InitGoogleTest(int*, char**)", referenced from:
      _main in cmtest.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
lipo: can't open input file: /var/tmp//ccZQiF8k.out (No such file or directory)

I have defined -arch i386 and -arch x86_64 for everything I have built so I am unable to figure out what I have missed. I don't do a lot of programming on Macs and this particular issue has me stuck.

Any suggestions would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尹雨沫 2025-01-10 00:12:29

找到解决方案。事实证明,使用的构建指令是正确的,问题在于,在执行构建的计算机上,另一位开发人员已经使用 Google Test Framework 1.5 完成了一些工作,并且他们已将库安装在计算机上的编译器搜索路径中。这导致编译器首先找到未使用多个体系结构选项编译的其他库。

Solution found. It turns out that the build instructions used are correct the issue was that on the computer that the build was being performed another developer had done some work with Google Test Framework 1.5 and they had installed the libraries on the computer in the compilers search path. This resulted in the compiler finding the other library first that was not compiled using multiple architecture options.

逆光下的微笑 2025-01-10 00:12:29

使用相同的编译器(-flags)编译 gtest 和您的项目。

如果您使用 Apple g++ 编译 gtest,同时使用 homebrew 安装 gcc,则链接到 gtest 将导致此错误。这正是 Google 建议在项目中集成静态链接的 gtest 而不是使用预编译的二进制文件的原因。

Compile gtest and your project with the same compiler (-flags).

If you compiled gtest with Apple g++ and meanwhile installed gcc with e.g. homebrew, linking to gtest will cause this error. This is exactly the reason why Google advices to integrate gtest statically linked in your project, and not to use precompiled binaries.

别挽留 2025-01-10 00:12:29

如果 gmock 和 gtest 不同步,您也可能会遇到此问题。我在 gmock-1.6.0 和 gtest-1.7.0 上遇到了类似的构建错误:

../../../third_party/gmock/build/libgmock.a(gmock-all.cc.o): In function `testing::internal::ParseGoogleMockFlagValue(char const*, char const*, bool)':
gmock-all.cc:(.text+0x3b5c): undefined reference to `testing::internal::String::Format(char const*, ...)'
../../../third_party/gmock/build/libgmock.a(gmock-all.cc.o): In function `testing::internal::String::operator==(char const*) const':
gmock-all.cc:(.text._ZNK7testing8internal6StringeqEPKc[testing::internal::String::operator==(char const*) const]+0x33): undefined reference to `testing::internal::String::Compare(testing::internal::String const&) const'
collect2: ld returned 1 exit status

切换到两者 1.7.0 或两者 1.6.0 解决了问题。

You might also have this problem if gmock and gtest are out of sync. I had similar build errors with gmock-1.6.0 and gtest-1.7.0:

../../../third_party/gmock/build/libgmock.a(gmock-all.cc.o): In function `testing::internal::ParseGoogleMockFlagValue(char const*, char const*, bool)':
gmock-all.cc:(.text+0x3b5c): undefined reference to `testing::internal::String::Format(char const*, ...)'
../../../third_party/gmock/build/libgmock.a(gmock-all.cc.o): In function `testing::internal::String::operator==(char const*) const':
gmock-all.cc:(.text._ZNK7testing8internal6StringeqEPKc[testing::internal::String::operator==(char const*) const]+0x33): undefined reference to `testing::internal::String::Compare(testing::internal::String const&) const'
collect2: ld returned 1 exit status

Switching to both 1.7.0 or both 1.6.0 fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文