运行 linux 上的自定义协议插件导致

Running custom protoc-plugin on linux causes

我正在构建一个基于 protobuf 的 google c++ 库的自定义协议编译器。
我 运行 在 linux 上 运行 时遇到了 st运行ge 错误,而在 MacOS运行 上没问题

terminate called after throwing an instance of 'std::system_error'
what():  Unknown error -1

设置并尝试使用我的调试器后,这是堆栈跟踪:

#1  0x00007f61c097b897 in abort () from /usr/lib/libc.so.6
#2  0x00007f61c0d1381d in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007f61c0d204da in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:47
#4  0x00007f61c0d20537 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:57
#5  0x00007f61c0d2078e in __cxxabiv1::__cxa_throw (obj=obj@entry=0x5568aec89df0, tinfo=tinfo@entry=0x7f61c0e5a750 <typeinfo for std::system_error>, dest=dest@entry=0x7f61c0d4cc60 <std::system_error::~system_error()>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6  0x00007f61c0d167ff in std::__throw_system_error (__i=-1) at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/new_allocator.h:89
#7  0x00007f61c14b5c63 in std::call_once<void (&)(google::protobuf::internal::DescriptorTable const*), google::protobuf::internal::DescriptorTable const*&> (__f=@0x7f61c14c0780: {void (const google::protobuf::internal::DescriptorTable *)} 0x7f61c14c0780 <google::protobuf::(anonymous namespace)::AssignDescriptorsImpl(google::protobuf::internal::DescriptorTable const*)>, __once=...) at /usr/include/c++/7/mutex:698
#8  google::protobuf::internal::AssignDescriptors (table=<optimized out>, table@entry=0x7f61c17dddc0 <descriptor_table_google_2fprotobuf_2fdescriptor_2eproto>) at google/protobuf/generated_message_reflection.cc:2407
#9  0x00007f61c148f440 in google::protobuf::FileDescriptorProto::GetMetadataStatic () at ./google/protobuf/descriptor.pb.h:623
#10 google::protobuf::FileDescriptorProto::GetMetadata (this=<optimized out>) at google/protobuf/descriptor.pb.cc:2281
#11 0x00005568acbb22ed in google::protobuf::Message::GetReflection (this=0x7ffd47e4efc0) at /home/leo/CLionProjects/protoc-gen-java-leo/protoc/include/google/protobuf/message.h:333
#12 0x00005568acbad61f in google::protobuf::compiler::java_leo::(anonymous namespace)::CollectExtensions (message=..., extensions=0x7ffd47e4eef0) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:84
#13 0x00005568acbad8ca in google::protobuf::compiler::java_leo::(anonymous namespace)::CollectExtensions (file_proto=..., alternate_pool=..., extensions=0x7ffd47e4eef0, file_data="\n1addressbook.proto2\btutorial27google/protobuf/timestamp.proto2\roptions.proto\"5[=13=]2\n[=13=]6Person2\"\n[=13=]2id0[=13=]1 [=13=]1(\tB22216java.util.UUIDR[=13=]2id22\n[=13=]4name0[=13=]2 [=13=]1(\tR[=13=]4name20\n[=13=]3age0[=13=]3 [=13=]1([=13=]5R[=13=]3age24\n[=13=]5email0[=13=]4 [=13=]1(\tR[=13=]5email24\n[=13=]6phones0[=13=]5 [=13=]3(\v24.tutorial.Person.PhoneNumberR[=13=]6phones2=\n\flast_updated0[=13=]6 [=13=]1(\v22.google.protobuf.TimestampR\vlastUpdated2N\n\vPhoneNumber26\n[=13=]6number0[=13=]1 [=13=]1(\tR[=13=]6number2'\n[=13=]4type0[=13=]2 [=13=]1(623.tutorial.PhoneTypeR[=13=]4type\"7\n\vAddressBook2(\n[=13=]6people0[=13=]1 [=13=]3(\v20.tutorial.PersonR[=13=]6people*+\n\tPhoneType2\n\n[=13=]6MOBILE0[=13=]02\b\n[=13=]4HOME0[=13=]12\b\n[=13=]4WORK0[=13=]2B+\n4com.example.tutorialB1AddressBookProtosP[=13=]1b[=13=]6proto3") at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:122
#14 0x00005568acbaf23f in google::protobuf::compiler::java_leo::FileGenerator::GenerateDescriptorInitializationCodeForImmutable (this=0x5568aec7d590, printer=0x7ffd47e4f380) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:439
#15 0x00005568acbaed2a in google::protobuf::compiler::java_leo::FileGenerator::Generate (this=0x5568aec7d590, printer=0x7ffd47e4f380) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:351
#16 0x00005568acbb73ea in google::protobuf::compiler::java_leo::JavaGenerator::Generate (this=0x7ffd47e4f788, file=0x5568aec77500, parameter="", context=0x7ffd47e4f5f0, error=0x7ffd47e4f5d0) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_generator.cc:158
#17 0x00007f61c0f06fae in google::protobuf::compiler::CodeGenerator::GenerateAll (this=0x7ffd47e4f788, files=std::vector of length 2, capacity 2 = {...}, parameter="", generator_context=0x7ffd47e4f5f0, error=0x7ffd47e4f5d0) at google/protobuf/compiler/code_generator.cc:58
#18 0x00007f61c0f16733 in google::protobuf::compiler::GenerateCode (request=..., generator=..., response=response@entry=0x7ffd47e4f6a0, error_msg=error_msg@entry=0x7ffd47e4f680) at google/protobuf/compiler/plugin.cc:133
#19 0x00007f61c0f16b17 in google::protobuf::compiler::PluginMain (argc=<optimized out>, argv=0x7ffd47e4f8c8, generator=0x7ffd47e4f788) at google/protobuf/compiler/plugin.cc:169
#20 0x00005568acbe9ed5 in main (argc=1, argv=0x7ffd47e4f8c8) at /home/leo/CLionProjects/protoc-gen-java-leo/main.cpp:10
#21 0x00007f61c097d153 in __libc_start_main () from /usr/lib/libc.so.6
#22 0x00005568acb9598e in _start ()

因为我是一个完全的 C++ 菜鸟,我花了大约 8 个小时来尝试和谷歌搜索才找到原因,所以我想在这里分享给下一个可能 运行 进入这个问题。

事实证明,"pthread"-library 是必需的,如果它不存在,它会在调用 "call_once"-method 时崩溃。更好的错误消息会更好,"what(): Unknown error -1" 没有太大帮助 :D

我所做的就是在 CMakeLists.txt:

中添加这一行
target_link_libraries(${CMAKE_PROJECT_NAME} pthread)

现在它运行起来就像一个魅力:)

希望这对某人有所帮助。