我目前正在尝试构建一些使用Google C ++ Test框架的测试代码,但我一直收到错误说明
ld:警告:在/usr/local/lib/libgtest.dylib中,文件是为不支持的文件格式构建的,而不是所链接的体系结构(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上做很多编程,这个特殊问题让我陷入困境。
任何建议都会有所帮助。
答案 0 :(得分:25)
找到解决方案。事实证明,所使用的构建指令是正确的,问题是在正在执行构建的计算机上,另一个开发人员已经使用Google Test Framework 1.5完成了一些工作,并且他们已经在编译器搜索路径中的计算机上安装了这些库。这导致编译器首先找到未使用多个体系结构选项编译的其他库。
答案 1 :(得分:4)
如果您使用Apple g ++编译gtest并同时安装了gcc,例如自制软件,链接到gtest会导致此错误。这正是Google建议在项目中静态链接gtest而不是使用预编译二进制文件的原因。
答案 2 :(得分:3)
如果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解决了问题。