使用GMock的Clang++未定义引用错误
Clang++ undefined reference errors with GMock
我昨天进行了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-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
导致许多与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::cout
和std::endl
。
我认为您是版本5中libstdc++引入的ABI更改的受害者。他们不得不改变std::string
的实现,因为C++11要求特定的实现,而以前不是这样。这会导致符号名称的更改。该问题特定于从v5之前的libstdc++(gcc)迁移到v5或更高版本,因此不应再次发生。
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++引用错误
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 只有级联分类器会发出未定义的引用错误
- 2个模板化类的非模板友元函数未定义引用错误
- C/C++ 取消引用错误:在空检查之前取消引用
- 为什么我的C++代码无法编译,出现未定义的引用错误
- 我的代码中C++未定义的引用错误?
- 模板专用化会导致未定义的引用错误
- .cpp文件中未定义的引用错误
- c++ 中目录中文件的未定义引用错误
- Opencv4 使用 pkg-config 给出未定义的引用错误
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- 共享指针取消引用错误
- 编译简单的多文件类程序时出现未定义的引用错误
- 模板函数产生未定义的引用错误
- 调用提取重加载器会产生对"运算符"的未定义引用错误>>
- 如何修复"未定义的引用"错误 opencv 和 g++
- 未定义的引用错误,我该怎么办?