compiling grpc c++ app get undefined reference to `event_store::Event::Event(google::protobuf::Arena*)'

Viewed 2210

Extremely new to C++, I am trying to compile a simple grpc app, have been trying few other ways to compile it but without success, including invoking g++ directly like below:

g++ main.cpp -std=c++17 -I./proto -l:uSockets.a -I/usr/local/include/uWebsockets -lz -lpthread -lgrpc -lprotobuf

I ran out of idea of how compile this, so any help will be much appreciated

here's my CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)

project(promotion)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(promotion main.cpp)
target_include_directories(promotion PUBLIC proto)
target_link_libraries(promotion z pthread :uSockets.a grpc protobuf)

when running the generated makefile from cmake, i get the errors like below:

Scanning dependencies of target promotion
[ 50%] Building CXX object CMakeFiles/promotion.dir/main.cpp.o
[100%] Linking CXX executable promotion
CMakeFiles/promotion.dir/main.cpp.o: In function `event_store::Event::Event()':
main.cpp:(.text._ZN11event_store5EventC2Ev[_ZN11event_store5EventC5Ev]+0x19): undefined reference to `event_store::Event::Event(google::protobuf::Arena*)'
CMakeFiles/promotion.dir/main.cpp.o: In function `event_store::EventResponse::EventResponse()':
main.cpp:(.text._ZN11event_store13EventResponseC2Ev[_ZN11event_store13EventResponseC5Ev]+0x19): undefined reference to `event_store::EventResponse::EventResponse(google::protobuf::Arena*)'
CMakeFiles/promotion.dir/main.cpp.o: In function `clients::EventStore::EventStore(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
main.cpp:(.text._ZN7clients10EventStoreC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7clients10EventStoreC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x25): undefined reference to `grpc::InsecureChannelCredentials()'
main.cpp:(.text._ZN7clients10EventStoreC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7clients10EventStoreC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x3c): undefined reference to `grpc::CreateChannel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::shared_ptr<grpc::ChannelCredentials> const&)'
main.cpp:(.text._ZN7clients10EventStoreC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7clients10EventStoreC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x72): undefined reference to `event_store::EventStore::NewStub(std::shared_ptr<grpc::ChannelInterface> const&, grpc::StubOptions const&)'
CMakeFiles/promotion.dir/main.cpp.o: In function `clients::EventStore::Publish(event_store::Event&, event_store::EventResponse*)':
main.cpp:(.text._ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE[_ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE]+0x33): undefined reference to `grpc::ClientContext::ClientContext()'
main.cpp:(.text._ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE[_ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE]+0x6c): undefined reference to `event_store::EventStore::Stub::Publish(grpc::ClientContext*, event_store::Event const&, event_store::EventResponse*)'
main.cpp:(.text._ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE[_ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE]+0x7c): undefined reference to `grpc::ClientContext::~ClientContext()'
main.cpp:(.text._ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE[_ZN7clients10EventStore7PublishERN11event_store5EventEPNS1_13EventResponseE]+0x90): undefined reference to `grpc::ClientContext::~ClientContext()'
CMakeFiles/promotion.dir/main.cpp.o: In function `_ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_':
main.cpp:(.text._ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_[_ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_]+0x3ff): undefined reference to `event_store::EventResponse::~EventResponse()'
main.cpp:(.text._ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_[_ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_]+0x40e): undefined reference to `event_store::Event::~Event()'
main.cpp:(.text._ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_[_ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_]+0x656): undefined reference to `event_store::EventResponse::~EventResponse()'
main.cpp:(.text._ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_[_ZZN8Handlers25HandlePromoCodeValidationEvENKUlPT_PT0_E_clIN3uWS12HttpResponseILb0EEENS6_11HttpRequestEEEDaS1_S3_]+0x66a): undefined reference to `event_store::Event::~Event()'
CMakeFiles/promotion.dir/main.cpp.o: In function `event_store::EventStore::Stub::experimental_async::~experimental_async()':
main.cpp:(.text._ZN11event_store10EventStore4Stub18experimental_asyncD2Ev[_ZN11event_store10EventStore4Stub18experimental_asyncD5Ev]+0xd): undefined reference to `vtable for event_store::EventStore::Stub::experimental_async'
CMakeFiles/promotion.dir/main.cpp.o: In function `event_store::EventStore::Stub::~Stub()':
main.cpp:(.text._ZN11event_store10EventStore4StubD2Ev[_ZN11event_store10EventStore4StubD5Ev]+0xd): undefined reference to `vtable for event_store::EventStore::Stub'
collect2: error: ld returned 1 exit status
CMakeFiles/promotion.dir/build.make:102: recipe for target 'promotion' failed
make[2]: *** [promotion] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/promotion.dir/all' failed
make[1]: *** [CMakeFiles/promotion.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2

I do have the libprotobuf.a in my /usr/local/lib

1 Answers

I apologise for this type of question, it was made out of desperation. It is very hard to get help without full context and often with full context, it requires one to put aside a significant time to debug and provide help.

Anyway, I got it to compile finally by observing the output of the makefile included in the grpc c++ hello world on github.

Instead of compiling it like the code I provided, the different codes have to be compiled individually before linked together, as below:

# compile the protobuf message 
g++ -std=c++17 `pkg-config --cflags protobuf grpc` -I./proto  -c -o event_store.pb.o proto/event_store/event_store.pb.cc

# compile the grpc service
g++ -std=c++17 `pkg-config --cflags protobuf grpc` -I./proto  -c -o event_store.grpc.pb.o proto/event_store/event_store.grpc.pb.cc 

# compile your own application
g++ -std=c++17 `pkg-config --cflags protobuf grpc` -I./proto  -c -o main.o main.cpp

# link the compiled protobuf message, grpc service and your own application
g++ -std=c++17 event_store.grpc.pb.o event_store.pb.o main.o -l:uSockets.a -lz -I/usr/local/include/uWebSockets -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o promotion

Note: in my case, I have the proto code generated separately and put in the ./proto folder hence -I./proto

and then profit:

./promotion

edit: I have managed to complete the app to a certain extent, and here's the repo to the full code for anyone who is interested

Related