I think this is related to C++ linker error than to thrift. I made a change to the thrift file and regenerated cpp & java classes. After this change, I started getting linker errors in cpp. Here is the error
Undefined symbols:
"com::XXXX::thrift::employee::SavingsInfo::operator<(com::XXXX::thrift::employee::SavingsInfo const&) const", referenced from:
std::less::operator()(com::XXXX::thrift::employee::SavingsInfo const&, com::XXXX::thrift::employee::SavingsInfo const&) constin employee_types.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [ThriftCPPSamples] Error 1
I added SavingsInfo type to thrift file, this is the change that I made. I give all the options mentioned in the doc to g++. I gave -I/usr/local/include/thrift
,
-I/path-to-boost
, -L/path-to-boost-lib
, -lthrift
. But after the change I started getting the above linker error. I couldn't understand the reason for this. The error points to something that was generated by thrift. What could be the reason for the error?
This error is about "operator <", so I am posting only the code relevant to it. Full code is available from the two links provided at the end.
employee_types.h
class SavingsInfo {
public:
std::string name;
double amount;
bool operator == (const SavingsInfo & rhs) const { /*...*/ }
bool operator != (const SavingsInfo &rhs) const { return !(*this == rhs); }
bool operator < (const SavingsInfo & ) const;
uint32_t read(::apache::thrift::protocol::TProtocol* iprot);
uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const;
};
class EmployeeInfo {
public:
int32_t id;
std::string name;
double salary;
bool contract;
std::set<std::string> dependents;
std::set<SavingsInfo> savings;
bool operator == (const EmployeeInfo & rhs) const { /*...*开发者_如何学Go/ }
bool operator != (const EmployeeInfo &rhs) const { return !(*this == rhs); }
bool operator < (const EmployeeInfo & ) const;
uint32_t read(::apache::thrift::protocol::TProtocol* iprot);
uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const;
};
employee_types.cpp
uint32_t EmployeeInfo::read(::apache::thrift::protocol::TProtocol* iprot) {
uint32_t xfer = 0;
std::string fname;
::apache::thrift::protocol::TType ftype;
int16_t fid;
xfer += iprot->readStructBegin(fname);
using ::apache::thrift::protocol::TProtocolException;
while (true)
{
xfer += iprot->readFieldBegin(fname, ftype, fid);
if (ftype == ::apache::thrift::protocol::T_STOP) {
break;
}
switch (fid)
{
// removed the case statements which deal with reading other fields
case 6:
if (ftype == ::apache::thrift::protocol::T_SET) {
{
this->savings.clear();
uint32_t _size6;
::apache::thrift::protocol::TType _etype9;
iprot->readSetBegin(_etype9, _size6);
uint32_t _i10;
for (_i10 = 0; _i10 < _size6; ++_i10)
{
SavingsInfo _elem11;
xfer += _elem11.read(iprot);
this->savings.insert(_elem11);
}
iprot->readSetEnd();
}
this->__isset.savings = true;
} else {
xfer += iprot->skip(ftype);
}
break;
default:
xfer += iprot->skip(ftype);
break;
}
xfer += iprot->readFieldEnd();
}
xfer += iprot->readStructEnd();
return xfer;
}
uint32_t EmployeeInfo::write(::apache::thrift::protocol::TProtocol* oprot) const {
uint32_t xfer = 0;
// removed the write statements for other fields
xfer += oprot->writeStructBegin("EmployeeInfo");
xfer += oprot->writeFieldBegin("savings", ::apache::thrift::protocol::T_SET, 6);
{
xfer += oprot->writeSetBegin(::apache::thrift::protocol::T_STRUCT,
this->savings.size());
std::set<SavingsInfo> ::const_iterator _iter13;
for (_iter13 = this->savings.begin(); _iter13 != this->savings.end(); ++_iter13)
{
xfer += (*_iter13).write(oprot);
}
xfer += oprot->writeSetEnd();
}
xfer += oprot->writeFieldEnd();
xfer += oprot->writeFieldStop();
xfer += oprot->writeStructEnd();
return xfer;
}
Couple of other things that I have tried:
- Instead of set<SavingsInfo>, if I use SavingsInfo, it works fine.
- Commented "operator <" in employee_types.h since I couldn't figure out where this was getting used. I got the following build error
Building file: ../src/employee_types.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/thrift -I/Users/raghava/Software/Boost_C++_Library/boost_1_43_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/employee_types.d" -MT"src/employee_types.d" -o"src/employee_types.o" "../src/employee_types.cpp"
/usr/include/c++/4.2.1/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = com::xxxx::thrift::employee::SavingsInfo]':
/usr/include/c++/4.2.1/bits/stl_tree.h:982: instantiated from 'std::pair::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = com::xxxx::thrift::employee::SavingsInfo, _Val = com::xxxx::thrift::employee::SavingsInfo, _KeyOfValue = std::_Identity, _Compare = std::less, _Alloc = std::allocator]'
/usr/include/c++/4.2.1/bits/stl_set.h:307: instantiated from 'std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = com::xxxx::thrift::employee::SavingsInfo, _Compare = std::less, _Alloc = std::allocator]'
../src/employee_types.cpp:163: instantiated from here
/usr/include/c++/4.2.1/bits/stl_function.h:227: error: no match for 'operator<' in '__x < __y'
make: *** [src/employee_types.o] Error 1
Complete source code of these 2 files is given in below links. The other link is in the comments (I cannot post it until I get 10 pts of reputation).
employee_types.cpp -- http://pastebin.com/7dLtstCK
employee_types.h -- http://pastebin.com/JGzE8V6JThank you.
Regards, Raghava.
精彩评论