- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我昨天进行了 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-dev
和 libstdc++-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::cout
和std::endl
.
最佳答案
我认为您是 v5 中随 libstdc++ 引入的 ABI 更改的受害者。他们不得不更改 std::string
的实现,因为 C++11 要求特定的实现,而以前并非如此。这导致符号名称发生变化。该问题特定于从 v5 之前的 libstdc++ (gcc) 迁移到 v5 或更高版本,因此不应再次发生。
关于c++ - GMock 的 Clang++ undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638525/
我在我的项目中使用 gmock,我遇到了为模拟函数设置自定义引用变量的问题。 假设我有一个类如下: class XXXClient { public: void QueryXXX(const
我正在尝试将 googlemock 集成到我的测试中。我已经在 googletest 上成功构建并运行了测试,现在我也在尝试逐步将 gmock 功能添加到测试中,但是我遇到了一个我完全不明白的编译错误
我正在进行一些测试,以便重构一个旧的 C++ 项目。我正在尝试使用 gmock 匹配器 ElementsAreArray() 来匹配两个数组。 EXPECT_THAT(value_instance.v
我正在尝试为向量编写测试。 对于 STL 容器,我尝试过: EXPECT_THAT(float_vec1, ElementsAreArray(float_vec2)); 但是我需要插入一个边距。 有没
我有一个具有接受可变参数的方法的类: class MyClass { public: virtual void myprint(const char* format, ...) = 0; }; 我试图
我想做这样的事情: EXPECT_CALL(*mock, method(5)).WillOnce(Return(arg1 * 2)); 其中 arg1 应等于被调用方法的第一个参数。有没有一种方法可以
我正在尝试模拟IAudioMeterInformation Windows API(我只需要GetPeakValue()方法)。 我收到一个错误: Error C2259 'AudioMeterInf
我使用GTest遇到了无法理解的行为。 问题很容易。我在叫sut方法。该方法调用StrictMock对象方法。我对该方法调用没有任何期望。根据GTest规范,此类测试应因“无趣的模拟函数调用”而失败。
我想做这样的事情: EXPECT_CALL(*mock, method(5)).WillOnce(Return(arg1 * 2)); 其中 arg1 应等于被调用方法的第一个参数。有没有一种方法可以
我有一个类,成员数组类型为int // Class Defenition class Foo { int array[5]; // ... Other Memebers } 另一个
我有一个类似这样的测试: #include using namespace ::testing; class IMyInterface { public: virtual ~IMyInter
我正在为一个类编写 GMOCK 测试用例: class A{ .. void Text() .. }; 现在A类的一个成员方法中嵌入了一个B类类型的对象,也引用了静态成员方法: void A::Tex
当我尝试像下面那样实现 GMOCK 方法时出现编译错误,但是当我删除它时代码和 MOCK 类工作正常: MOCK_METHOD2(myfunc, void (std::shared_ptr, std:
示例代码: struct MyFixture: public ::testing::Test { }; template struct MyFixtureWithParam: public MyFi
在 gmock 中是否有匹配类型而不是值的方法?该类是这样的: struct Blob { template bool is(); // if blob holds data of type
我刚开始用 gtest 和 gmock 做测试,然后我被一个问题阻止了。问题是这样的, 首先,我有一个这样的类, class Foo { public: std::vector& GetVec
我有一个测试对象,我想在真实对象中不需要的方法上调用 EXPECT_CALL,是否可以模拟这种未定义的新方法? struct MockObject { MOCK_METHOD2(onRecv,
我有以下模拟方法: MOCK_METHOD1(send, void(const std::vector& data)); 如何检查是否使用特定参数调用该方法,例如 std::vector vec{1,
我看到了一些关于 gmock 的示例代码, #include "gmock/gmock.h" #include "gtest/gtest.h" #include #include class Mo
这个问题在这里已经有了答案: Possible memory leak without a virtual destructor? (3 个答案) 关闭 5 年前。 代码 class A { pu
我是一名优秀的程序员,十分优秀!