I am writing a program using gRPC in the language c++. I have installed gRPC using the command brew install grpc
on a MacOS I also have the most up to date version of protobuf
. When I create a basic program using gRPC I compile them using the following commands:
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/test.proto
Which creates the test.pb.h
and test.pb.cc
files
Then the command protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/Cellar/grpc/1.45.2/bin/grpc_cpp_plugin test.proto
which creates the test.pb.h
and test.pb.cc
files
I then run the command g++ -std=c++11 -stdlib=libc++ test.cpp test.grpc.pb.cc -L/usr/local/lib -lprotobuf
to compile, where the main c++ file is test.cpp
I then get some of the following linkage errors:
"grpc::ClientContext::ClientContext()", referenced from:
MathTestClient::sendRequest(int, int) in test-e2eddc.o
"grpc::ClientContext::~ClientContext()", referenced from:
MathTestClient::sendRequest(int, int) in test-e2eddc.o
"grpc::CreateChannel(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<grpc::ChannelCredentials> const&)", referenced from:
Run() in test-e2eddc.o
"grpc::InsecureChannelCredentials()", referenced from:
Run() in test-e2eddc.o
"mathtest::MathRequest::MathRequest(google::protobuf::Arena*, bool)", referenced from:
mathtest::MathRequest::MathRequest() in test-e2eddc.o
"mathtest::MathRequest::~MathRequest()", referenced from:
MathTestClient::sendRequest(int, int) in test-e2eddc.o
Which makes sense, judging based off the fact that in the file grcpp/impl/codgen/client_context.h
the linkage errors would obviously be there because client context is defined as the following:
class ClientContext {
public:
ClientContext();
~ClientContext();
or with the CreateChannel
function it is defined in create_channel.h
file as
std::shared_ptr<Channel> CreateChannel(
const grpc::string& target,
const std::shared_ptr<ChannelCredentials>& creds);
In my main c++ file, I only use the following include from gRPC: #include <grpcpp/grpcpp.h>
Are there other include files from gRPC that I need to include which define these functions?
I ended up figuring out my problem if anyone else has installed grpc the same way using homebrew as I did. make sure that on top of the
brew install grpc
command is run that the commandsbrew install protobuf
andbrew install abseil
Next is to find out the paths of all of these using the commands
brew info grpc
,brew info protobuf
andbrew info abseil
for myself personally the paths are:/usr/local/Cellar/abseil/20211102.0
/usr/local/Cellar/grpc/1.45.2
/usr/local/Cellar/protobuf/3.19.4
Then the
$PKG_CONFIG_PATH
needs to be setup, if this does not already exist and there is an error if you runecho $PKG_CONFIG_PATH
you can runbrew install pkg-config
the$PKG_CONFIG_PATH
is defined by all the previous paths, with the added path of/lib/pkgconfig
which then the$PKG_CONFIG_PATH
can be defined asexport PKG_CONFIG_PATH=/usr/local/Cellar/grpc/1.45.2/lib/pkgconfig:/usr/local/Cellar/protobuf/3.19.4/lib/pkgconfig:/usr/local/Cellar/abseil/20211102.0/lib/pkgconfig
After that is all done, and you have the main c++ file and the proto file, mine were named
test.cpp
andtest.proto
respectively, the following commands can be run to make it compile correctly and be able to get and then run./test