使用GMock的Clang++未定义引用错误

Clang++ undefined reference errors with GMock

本文关键字:引用 错误 未定义 Clang++ GMock 使用      更新时间:2023-10-16

我昨天进行了dist升级,现在我在使用GMock和clang++编译测试时遇到了未定义的引用错误;不过,它在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)

制造商:***[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导致许多与gmock。

我似乎无法链接到使用-l-L的旧版本。正在搜索around显示的结果建议不要尝试静态链接这些而使用旧版本的编译器。但是,编译器根据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无论如何,CCD_ 12。

最后,我试着从他们的网站上为clang-3.8使用预编译的二进制文件,但它给了我一个巨大的未定义引用std::<一切>。我确实得到了helloworld程序进行编译,这样它至少可以找到std::coutstd::endl

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