我对编程和阿夫罗很陌生。所以,请耐心地容忍我。我试图用AVRO格式对几个字符串进行编码和解码。我遵循了APACHE页面中提到的相同的示例。参考资料:- https://avro.apache.org/docs/1.10.2/api/cpp/html/index.html .
这是我的JSON模式。
{
"type": "record",
"name": "cpx",
"fields" : [
{"name": "re", "type": "string"},
{"name": "im", "type" : "string"}
]
}这是我生成的头文件。
namespace c {
struct cpx {
std::string re;
std::string im;
cpx() :
re(std::string()),
im(std::string())
{ }
};
}
namespace avro {
template<> struct codec_traits<c::cpx> {
static void encode(Encoder& e, const c::cpx& v) {
avro::encode(e, v.re);
avro::encode(e, v.im);
}
static void decode(Decoder& d, c::cpx& v) {
if (avro::ResolvingDecoder *rd =
dynamic_cast<avro::ResolvingDecoder *>(&d)) {
const std::vector<size_t> fo = rd->fieldOrder();
for (std::vector<size_t>::const_iterator it = fo.begin();
it != fo.end(); ++it) {
switch (*it) {
case 0:
avro::decode(d, v.re);
break;
case 1:
avro::decode(d, v.im);
break;
default:
break;
}
}
} else {
avro::decode(d, v.re);
avro::decode(d, v.im);
}
}
};}
这是我的CPP源文件。
#include <stdlib.h>
#include "bla.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
int main()
{
std::unique_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
c::cpx c1;
c1.re = "hello";
c1.im = "thanks";
avro::encode(*e, c1);
std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
avro::DecoderPtr d = avro::binaryDecoder();
d->init(*in);
c::cpx c2;
avro::decode(*d, c2);
std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
std::cout << "DECODED" << std::endl;
return 0;
}我就是这样编译我的二进制文件的
g++ test.cpp -std=c++11 -lavrocpp
当我运行这个的时候,我有一个崩溃,这是我的核心垃圾堆。
root@2-9 bin_kw# gdb ./a.out /data/storage/corefile/core.a.out
根@2-9 bin_kw# gdb
./a.out /data/storage/corefiles/core.a.out.30222.2-9.com.1658156296 GNU gdb (GDB) Red Hat Enterprise Linux7.6.1-115.el7版权(C) 2013免费软件基金会(C)许可GPLv3+:GNU第3版或更高版本http://gnu.org/licenses/gpl.html这是免费软件:您可以自由地更改和重新分发它。在法律允许的范围内,没有任何保证。输入“显示复制”和“显示保修”以获取详细信息。这个GDB被配置为"x86_64-redhat-linux-gnu“。有关错误报告说明,请参见:http://www.gnu.org/software/gdb/bugs/.从/root/Nightly_Suite_Workspace/MS/CU_CP/bin_kw/a.out...(no调试符号中读取符号(找到)...done。使用主机libthread_db库“/lib64 64/lib线程_db.so.1”的新LWP 30222。核心由`./a.out‘生成。程序以信号11终止,分割故障。#0 ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:896 896处的__memcpy_ssse3_back () movaps 0x37(%rsi),%xmm5 5警告: File“/usr/local/lib64 64/libstdc+.so.so.0.21-gdb.py”自动加载已被设置为"$debugdir:$datadir/auto-load:/usr/bin/mono-gdb.py".的“自动加载安全路径”所拒绝。为了能够执行这个文件,向配置文件“/root/..gdbinit”中添加添加-自动加载安全路径/usr/local/ line 64/libstdc++.so.6.0.21-gdb.py行。要完全禁用这种安全保护,将set自动加载安全路径/行添加到配置文件“/root/..gdbinit”中。有关此安全保护的详细信息,请参阅GDB手册中的“自动加载安全路径”一节。例如,从shell运行: info“(Gdb)自动加载安全路径”缺少单独的调试器,使用:调试器-安装boost-文件系统-1.53.0-28.el7.x86_64 boost-iostreams-1.53.0-28.el7.x86_64 boost-程序-选项-1.53.0-28.el7.x86_64 boost-regex-1.53.0-28.el7.x86_64 boost-系统-1.53.0-28.el7.x86_64 bzip2-libs-1.0.6-13.el7.x86_64 libicu 50.2-3.el7.x86_64
(gdb) bt
在avro中的../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:896 #1 0x00007f28e9dad9e1处#0 __memcpy_ssse3_back ():BinaryEncoder::encodeString(std::string&) ()来自/usr/local/lib/libavrocpp.so.1.11.0 #2 0x0000000000401778 in avro::codec_traits *编码(avro::编码器&,std::__cxx11::basic_string const&) () #3 0x0000000000401a6e in avro::encode>(avro:编码器&,std::__cxx11::basic_string const&) () #4 0x0000000000401830 ( avro::codec_traits::encode(avro::Encoder&,c::cpx const&) () #5 0x0000000000401ddc in void avro::encode(avro:编码器&,c:cpx const&) (主() ) #6 0x00000000004014ac
我试着想办法,但却找不到任何线索。请帮我,谢谢。这就是我指的那一页。
发布于 2022-07-18 16:57:59
我从链接中安装了库,并构建了代码,大部分代码未经修改:
#include <string>
//#include "bla.hh"
#include "api/Compiler.hh"
#include "api/DataFile.hh"
#include "api/Decoder.hh"
#include "api/Encoder.hh"
#include "api/Generic.hh"
#include "api/Stream.hh"
namespace c {
struct cpx {
std::string re;
std::string im;
cpx() :
re(std::string()),
im(std::string())
{ }
};
}
namespace avro {
template<>
struct codec_traits<c::cpx>
{
static void encode(Encoder& e, const c::cpx& v)
{
avro::encode(e, v.re);
avro::encode(e, v.im);
}
static void decode(Decoder& d, c::cpx& v)
{
if(avro::ResolvingDecoder* rd =
dynamic_cast<avro::ResolvingDecoder*>(&d))
{
const std::vector<size_t> fo = rd->fieldOrder();
for(std::vector<size_t>::const_iterator it = fo.begin();
it != fo.end();
++it)
{
switch(*it)
{
case 0: avro::decode(d, v.re); break;
case 1: avro::decode(d, v.im); break;
default: break;
}
}
} else
{
avro::decode(d, v.re);
avro::decode(d, v.im);
}
}
};
} // namespace avro
#include <stdlib.h>
int main()
{
std::unique_ptr<avro::OutputStream> out =
avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
c::cpx c1;
c1.re = "hello";
c1.im = "thanks";
avro::encode(*e, c1);
std::unique_ptr<avro::InputStream> in =
avro::memoryInputStream(*out);
avro::DecoderPtr d = avro::binaryDecoder();
d->init(*in);
c::cpx c2;
avro::decode(*d, c2);
std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
std::cout << "DECODED" << std::endl;
return 0;
}它看上去很好,而且印得很好
(hello, thanks)
DECODED我甚至为整个项目(包括avro/lang/c++/CMakeLists.txt)启用了未定义的-fsanitize=address,而且它仍然干净地运行。
您可以通过静态链接来尝试:
g++ test.cpp -std=c++11 -lavrocpp_s如果这个可以运行,那么在构建时,您很可能会发现一个与在运行时安装和找到的版本不同的avrocpp。您还可以通过使用
ldd a.out并查看libavrocpp.so在运行时解析了哪些共享库。
https://stackoverflow.com/questions/73024824
复制相似问题