I am making an attempt to construct a gRPC consumer for LND in C++, beginning with the WalletBalance perform. I’ve by no means used gRPC earlier than, and I take advantage of make as a substitute of cmake, however in keeping with the documentation that is imagined to be doable. I’m utilizing the pkg-config information that get put in with gRPC, however they aren’t working.
Here’s what I’ve efficiently been ready to take action far.
- Construct gRPC
- Compile lightning.proto
- Compile
lightning.pb.ccandlightning.grpc.pb.cc - Hyperlink with
lightning.pb.cc.o(however notlightning.grpc.pb.cc.o)
That is my cpp file:
#embrace <iostream>
#embrace <string>
#embrace <grpcpp/grpcpp.h>
#embrace "lightning.grpc.pb.h"
#embrace "lightning.pb.h"
utilizing namespace std;
utilizing namespace lnrpc;
utilizing grpc::Channel;
utilizing grpc::ClientContext;
utilizing grpc::Standing;
class LightningClient
{
personal:
unique_ptr<Lightning::Stub> stub_;
public:
LightningClient (shared_ptr<grpc::Channel> channel) : stub_ (Lightning::NewStub (channel)) {}
unsigned lengthy WalletBalance (const string& person)
{
WalletBalanceRequest request;
WalletBalanceResponse reply;
ClientContext context;
Standing standing = stub_->WalletBalance (&context, request, &reply);
if (standing.okay ())
return reply.total_balance ();
else
cout << standing.error_code () << ": " << standing.error_message () << endl;
return 0;
}
};
int foremost (int argc, char *argv [])
{
shared_ptr<grpc::Channel> channel;// = CreateChannel ("localhost:10009", grpc::InsecureChannelCredentials ());
return 0;
}
That is my makefile:
lnd-grpc-client : lnd-grpc-client.o lightning.pb.cc.o lightning.grpc.pb.cc.o
g++-10 -pthread -o lnd-grpc-client lnd-grpc-client.o lightning.pb.cc.o `pkg-config --libs --static protobuf`
lightning.grpc.pb.cc.o : lightning.grpc.pb.cc
g++-10 -std=c++20 `pkg-config protobuf --cflags` -c -o lightning.grpc.pb.cc.o lightning.grpc.pb.cc
lightning.pb.cc.o : lightning.pb.cc
g++-10 -std=c++20 `pkg-config protobuf --cflags` -c -o lightning.pb.cc.o lightning.pb.cc
lnd-grpc-client.o : lnd-grpc-client.cpp
g++-10 -std=c++20 `pkg-config protobuf --cflags` -c -o lnd-grpc-client.o lnd-grpc-client.cpp
All of that compiles and hyperlinks simply advantageous. However I’ve the CreateChannel perform commented out in my cpp file and I’m not linking with lightning.grpc.pb.cc.o, each of which might be required if I’m to get this working.
If I uncomment the CreateChannel perform, it provides me a linker error saying undefined reference to 'grpc::InsecureChannelCredentials()'. However there doesn’t appear to be any pkg-config that solves that drawback.
If I attempt to hyperlink with lightning.grpc.pb.cc.o, I get a whole lot of linker errors. I can not publish all of them, however listed here are a number of:
lightning.grpc.pb.cc:(.textual content._ZN4grpc5SliceD2Ev[_ZN4grpc5SliceD5Ev]+0x20): undefined reference to 'grpc_slice_unref'
/usr/bin/ld: lightning.grpc.pb.cc.o: in perform 'grpc::Slice::Slice(unsigned lengthy)':
lightning.grpc.pb.cc:(.textual content._ZN4grpc5SliceC2Em[_ZN4grpc5SliceC5Em]+0x32): undefined reference to 'grpc_slice_malloc'
/usr/bin/ld: lightning.grpc.pb.cc.o: in perform 'grpc::SliceReferencingString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
lightning.grpc.pb.cc:(.textual content._ZN4grpc22SliceReferencingStringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4grpc22SliceReferencingStringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x50): undefined reference to 'grpc_slice_from_static_buffer'
/usr/bin/ld: lightning.grpc.pb.cc.o: in perform 'grpc::ByteBuffer::ByteBuffer(grpc::Slice const*, unsigned lengthy)':
lightning.grpc.pb.cc:(.textual content._ZN4grpc10ByteBufferC2EPKNS_5SliceEm[_ZN4grpc10ByteBufferC5EPKNS_5SliceEm]+0x27): undefined reference to 'grpc_raw_byte_buffer_create'
/usr/bin/ld: lightning.grpc.pb.cc.o: in perform 'grpc::ByteBuffer::~ByteBuffer()':
lightning.grpc.pb.cc:(.textual content._ZN4grpc10ByteBufferD2Ev[_ZN4grpc10ByteBufferD5Ev]+0x27): undefined reference to 'grpc_byte_buffer_destroy'
/usr/bin/ld: lightning.grpc.pb.cc.o: in perform 'grpc::ByteBuffer::Clear()':
lightning.grpc.pb.cc:(.textual content._ZN4grpc10ByteBuffer5ClearEv[_ZN4grpc10ByteBuffer5ClearEv]+0x27): undefined reference to 'grpc_byte_buffer_destroy'
I attempted including the grpc and grpc++ pkg-config information to my makefile’s hyperlink command, however that didn’t remedy the issue.
Has anybody really constructed a gRPC consumer for LND in C++? In that case, how have you learnt which libraries to hyperlink with and the way do you hyperlink with them?
