user2093191 user2093191 - 7 days ago 5
C++ Question

How to compile Apache Avro C++ example

Probably a newbie mistake, but can anyone please tell me what I'm doing wrong here?

Any help is much appreciated.

I wrote this simple Makefile:

CC=g++
INC=-I/usr/local/avro-cpp-1.7.2 -I/usr/local/boost_1_53_0

cpx : generated.cc
$(CC) -o cpx generated.cc $(INC)


Which generates these the errors:

g++ -o cpx generated.cc -I/usr/local/avro-cpp-1.7.2 -I/usr/local/boost_1_53_0
/tmp/ccYymUVo.o: In function `main':
generated.cc:(.text+0x84): undefined reference to `avro::memoryOutputStream(unsigned long)'
generated.cc:(.text+0xb0): undefined reference to `avro::binaryEncoder()'
generated.cc:(.text+0x11e): undefined reference to `avro::memoryInputStream(avro::OutputStream const&)'
generated.cc:(.text+0x150): undefined reference to `avro::binaryDecoder()'
collect2: ld returned 1 exit status
make: *** [cpx] Error 1


Here's the source as supplied in the Avro examples directory:

#############################
# cpx.hh
#############################
#ifndef CPX_HH_1278398428__H_
#define CPX_HH_1278398428__H_

#include "boost/any.hpp"
#include "avro/Specific.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"

namespace c {
struct cpx {
double re;
double im;
};

}
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) {
avro::decode(d, v.re);
avro::decode(d, v.im);
}
};

}
#endif


and

#############################
# generated.cc
#############################
#include "cpx.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"

int
main()
{
std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
c::cpx c1;
c1.re = 1.0;
c1.im = 2.13;
avro::encode(*e, c1);

std::auto_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;
return 0;
}


Thanks.

Answer

Try using avrogencpp with options -i < schema_file > -o < header_file >