gpt4 book ai didi

c++ - GMock 的 Clang++ undefined reference 错误

转载 作者:行者123 更新时间:2023-11-30 05:28:49 28 4
gpt4 key购买 nike

我昨天进行了 dist-upgrade,现在我在使用 GMock 和 clang++ 编译测试时遇到 undefined reference 错误;虽然它与 g++ 一起工作正常。GTest 和 GMock 从源代码编译并使用 Cmake 安装。

我想知道是否有人知道为什么链接不正确?

我正在编译:

clang++-3.7 -Wall -Wextra -pedantic -std=c++11 -Wshadow a.cpp -o a.out -lgtest -lgmock -pthread

我遇到了这些错误:

/tmp/a-bb74fa.o: In function `testing::internal::FunctionMockerBase<int ()>::DescribeDefaultActionTo(std::tuple<> const&, std::ostream*) const':
a.cpp:(.text._ZNK7testing8internal18FunctionMockerBaseIFivEE23DescribeDefaultActionToERKSt5tupleIJEEPSo[_ZNK7testing8internal18FunctionMockerBaseIFivEE23DescribeDefaultActionToERKSt5tupleIJEEPSo]+0x8e): undefined reference to `testing::internal::FormatFileLocation(char const*, int)'
/tmp/a-bb74fa.o: In function `testing::internal::ExpectationBase::DescribeLocationTo(std::ostream*) const':
a.cpp:(.text._ZNK7testing8internal15ExpectationBase18DescribeLocationToEPSo[_ZNK7testing8internal15ExpectationBase18DescribeLocationToEPSo]+0x43): undefined reference to `testing::internal::FormatFileLocation(char const*, int)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

make: *** [a.out] 错误 1

源代码:

#include<gtest/gtest.h>
#include<gmock/gmock.h>
#include <iostream>

class printable{
public:
virtual int print() = 0;
};

class A{
public:
A( printable* b ) :
obj( b )
{}
int doit(){return obj->print();}
private:
printable* obj;
};

class MockPrintable : public printable{
public:
MOCK_METHOD0( print, int() );
};

TEST( test, returnStuff ){
MockPrintable b;
A a( &b );
EXPECT_CALL( b, print() )
.WillOnce( testing::Return( 1 ) )
.WillOnce( testing::Return( 2 ) )
.WillOnce( testing::Return( 3 ) );
EXPECT_EQ( 1,a.doit() );
EXPECT_EQ( 2,a.doit() );
EXPECT_EQ( 3,a.doit() );
}

int main( int argc, char* argv[] ){
::testing::InitGoogleMock( &argc, argv );
return RUN_ALL_TESTS();
}

编辑:

我已经将问题缩小到 libgcc-5-devlibstdc++-5-dev包。

libstdc++-5-dev 导致此错误:

/usr/local/include/gtest/gtest.h:54:10: fatal error: 'limits' file not found
#include <limits>

libstdc++-5-dev 会导致很多undefined reference错误格莫克。

我似乎无法使用 -l-L 链接到旧版本。搜索中周围出现的结果建议不要尝试静态链接这些并改用旧版本的编译器。但是,编译器根据 apt-get,依赖项现在需要相同的 *-5-dev 包。

你可以通过将这个插入到来阻止 apt-get 使用这些包/etc/apt/preferences:

Package: libgcc-5-dev
Pin: release *
Pin-Priority: -1

Package: libstdc++-5-dev
Pin: release *
Pin-Priority: -1

只是 dist-upgrade 从那里开始,但它阻止了 g++、clang 和其他东西从更新所以这是不值得的; apt-get 建议删除 clang 作为一部分dist-upgrade 无论如何。

最后,我尝试使用他们网站上的 clang-3.8 的预编译二进制文件,但它给了我一个对 std::< everything > 的 undefined reference 的巨大墙。我确实得到了尽管要编译 hello world 程序,但它至少可以找到 std::coutstd::endl.

最佳答案

我认为您是 v5 中随 libstdc++ 引入的 ABI 更改的受害者。他们不得不更改 std::string 的实现,因为 C++11 要求特定的实现,而以前并非如此。这导致符号名称发生变化。该问题特定于从 v5 之前的 libstdc++ (gcc) 迁移到 v5 或更高版本,因此不应再次发生。

关于c++ - GMock 的 Clang++ undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638525/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com