简体   繁体   English

编译 grpc c++ 应用程序获得对`event_store::Event::Event(google::protobuf::Arena*)'的未定义引用

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

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:对 C++ 非常陌生,我正在尝试编译一个简单的 grpc 应用程序,一直在尝试其他几种方法来编译它但没有成功,包括直接调用 g++ 如下所示:

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 :这是我的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:从 cmake 运行生成的makefile时,我收到如下错误:

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我的/usr/local/liblibprotobuf.a

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 .无论如何,我通过观察github上的 grpc c++ hello world 中包含的makefile的输出,最终编译了它。

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注意:就我而言,我将 proto 代码单独生成并放在./proto文件夹中,因此-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编辑:我已经在一定程度上完成了应用程序,这里是任何有兴趣的人的完整代码的回购

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM