Protobuf ShortDebugString()崩溃

编码的

我有以下原型:

syntax = "proto3";
package pb;

message FooBar {
  string foo = 1;
  string bar = 2;
}

message FooBarList {
  repeated FooBar foobar = 1;
}

我只是用2个值填充重复列表,然后尝试将其打印为以下内容:

#include <iostream>
#include "foobar.pb.h"

int main()
{
    pb::FooBarList foobar_list;

    auto foobar1 = foobar_list.add_foobar();
    foobar1->set_foo("foo1");
    foobar1->set_bar("bar1");

    auto foobar2 = foobar_list.add_foobar();
    foobar2->set_foo("foo2");
    foobar2->set_bar("bar2");

    try {
        std::cout << foobar_list.ShortDebugString() << std::endl;
    }
    catch (const std::exception& e) {
        std::cout << "error: " << e.what() << std::endl;
        return -1;
    }

    return 0;
}

这将产生以下输出:

error: Unknown error -1

因此,ShortDebugString()在没有任何有意义的信息的情况下触发异常。
我是在做错什么,还是在调用之前需要某种方式触发序列化ShortDebugString()

这是callstack我让exeption传播的gdb

Program received signal SIGABRT, Aborted.
0x00007ffff7add355 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7add355 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7ac6853 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7e5d86a in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7e69d8a in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#4  0x00007ffff7e69df7 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#5  0x00007ffff7e6a09e in __cxxabiv1::__cxa_throw (obj=obj@entry=0x5555556e1550, 
    tinfo=tinfo@entry=0x7ffff7f97590 <typeinfo for std::system_error>, dest=dest@entry=0x7ffff7e96860 <std::system_error::~system_error()>)
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6  0x00007ffff7e609bc 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  0x00005555555a1018 in google::protobuf::internal::AssignDescriptors(google::protobuf::internal::DescriptorTable const*, bool) ()
#8  0x000055555556e77f in pb::FooBarList::GetMetadataStatic() ()
#9  0x000055555556ceb9 in pb::FooBarList::GetMetadata() const ()
#10 0x00005555555d1fb4 in google::protobuf::TextFormat::Printer::Print(google::protobuf::Message const&, google::protobuf::TextFormat::Printer::TextGenerator*) const ()
#11 0x00005555555d3ef9 in google::protobuf::Message::ShortDebugString[abi:cxx11]() const ()
#12 0x00005555555705ac in main ()
(gdb) 

编辑

这是CMakeLists.txt用来构建这个项目的:

cmake_minimum_required(VERSION 3.10)
project(proto-foobar)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(Protobuf_USE_STATIC_LIBS ON)
find_package(Protobuf MODULE REQUIRED)

set(PROTO_DEFINITIONS_PATH "${CMAKE_SOURCE_DIR}/protos")
set(PROTO_DEFINITIONS "${PROTO_DEFINITIONS_PATH}/*.proto")
set(PROTO_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/source")
file(MAKE_DIRECTORY ${PROTO_OUTPUT_PATH})

set(PROTO_HEADERS "${PROTO_OUTPUT_PATH}/foobar.pb.h")
set(PROTO_SOURCES "${PROTO_OUTPUT_PATH}/foobar.pb.cc")

add_custom_command(OUTPUT ${PROTO_HEADERS} ${PROTO_SOURCES}
                   COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
                   ARGS --cpp_out=${PROTO_OUTPUT_PATH} 
                        --proto_path=${PROTO_DEFINITIONS_PATH} 
                        ${PROTO_DEFINITIONS}
                   DEPENDS ${PROTO_DEFINITIONS}
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                   COMMENT "Compiling ${PROJECT_NAME} protos")    

file(GLOB_RECURSE PROJECT_FILES "${CMAKE_SOURCE_DIR}/source/*.cpp")
add_executable(${PROJECT_NAME} ${PROTO_SOURCES} ${PROJECT_FILES})

target_link_libraries(${PROJECT_NAME}       PUBLIC ${PROTOBUF_LIBRARY})
target_include_directories(${PROJECT_NAME}  PUBLIC ${PROTO_OUTPUT_PATH})

这是项目树:

├── CMakeLists.txt
├── protos
│   └── foobar.proto
└── source
    ├── foobar.pb.cc
    ├── foobar.pb.h
    └── main.cpp
Thkala

(遇到了完全相同的问题,并对其进行了深入研究。)

在这种情况下,触发器就是这段代码它包含(间接)对的调用std::call_once,在Linux上使用来实现pthread_once反过来,这要求您的二进制文件针对pthread库进行-lpthread链接(即,它需要一个链接器标志)。

如果您的二进制文件未链接到pthread您,则会得到您看到的错误,如此处所述并附有解决此错误的方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章