Protocol Buffers implementation in C

Overview

Build Status Coverage Status

Overview

This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c, a pure C library that implements protobuf encoding and decoding, and protoc-c, a code generator that converts Protocol Buffer .proto files to C descriptor code, based on the original protoc. protobuf-c formerly included an RPC implementation; that code has been split out into the protobuf-c-rpc project.

protobuf-c was originally written by Dave Benson and maintained by him through version 0.15 but is now being maintained by a new team. Thanks, Dave!

Mailing list

protobuf-c's mailing list is hosted on a Google Groups forum. Subscribe by sending an email to [email protected].

Building

protobuf-c requires a C compiler, a C++ compiler, protobuf, and pkg-config to be installed.

./configure && make && make install

If building from a git checkout, the autotools (autoconf, automake, libtool) must also be installed, and the build system must be generated by running the autogen.sh script.

./autogen.sh && ./configure && make && make install

Documentation

See the online Doxygen documentation here or the Wiki for a detailed reference. The Doxygen documentation can be built from the source tree by running:

make html

Synopsis

Use the protoc command to generate .pb-c.c and .pb-c.h output files from your .proto input file. The --c_out options instructs protoc to use the protobuf-c plugin.

protoc --c_out=. example.proto

Include the .pb-c.h file from your C source code.

#include "example.pb-c.h"

Compile your C source code together with the .pb-c.c file. Add the output of the following command to your compile flags.

pkg-config --cflags 'libprotobuf-c >= 1.0.0'

Link against the libprotobuf-c support library. Add the output of the following command to your link flags.

pkg-config --libs 'libprotobuf-c >= 1.0.0'

If using autotools, the PKG_CHECK_MODULES macro can be used to detect the presence of libprotobuf-c. Add the following line to your configure.ac file:

PKG_CHECK_MODULES([PROTOBUF_C], [libprotobuf-c >= 1.0.0])

This will place compiler flags in the PROTOBUF_C_CFLAGS variable and linker flags in the PROTOBUF_C_LDFLAGS variable. Read more information here about the PKG_CHECK_MODULES macro.

Versioning

protobuf-c follows the Semantic Versioning Specification as of version 1.0.0.

Note that as of version of 1.0.0, the header files generated by the protoc-c compiler contain version guards to prevent incompatibilities due to version skew between the .pb-c.h files generated by protoc-c and the public protobuf-c.h include file supplied by the libprotobuf-c support library. While we will try not to make changes to protobuf-c that will require triggering the version guard often, such as releasing a new major version of protobuf-c, this cannot be guaranteed. Thus, it's a good idea to recompile your .pb-c.c and .pb-c.h files from their source .proto files with protoc-c as part of your build system, with proper source file dependency tracking, rather than shipping potentially stale .pb-c.c and .pb-c.h files that may not be compatible with the libprotobuf-c headers installed on the system in project artifacts like repositories and release tarballs. (Note that the output of the protoc-c code generator is not standalone, as the output of some other tools that generate C code is, such as flex and bison.)

Major API/ABI changes may occur between major version releases, by definition. It is not recommended to export the symbols in the code generated by protoc-c in a stable library interface, as this will embed the protobuf-c ABI into your library's ABI. Nor is it recommended to install generated .pb-c.h files into a public header file include path as part of a library API, as this will tie clients of your library's API to particular versions of libprotobuf-c.

Contributing

Please send patches to the protobuf-c mailing list or by opening a GitHub pull request.

The most recently released protobuf-c version is kept on the master branch, while the next branch is used for commits targeted at the next release. Please base patches and pull requests against the next branch, not the master branch.

Copyright to all contributions are retained by the original author, but must be licensed under the terms of the BSD-2-Clause license. Please add a Signed-off-by header to your commit message (git commit -s) to indicate that you are licensing your contribution under these terms.

Comments
  • proto3 support

    proto3 support

    This is an experimental first cut at adding proto3 support and I have not tested it further than porting to proto3 the testcase. Before pushing this further I would like to have some feedback on whether I am on the right track.

    As far as I understand protobuf-c already has pretty much everything needed once it is built using a new version of protibuf itself. The only missing thing is that in proto3 all fields are optional and having to manually set has_foo is inconvenient.

    This patch special cases the proto3 syntax files so that structs for the bytes, enum and primitive fields do not emit the has_ field.

    It also adds PROTOBUF_C_LABEL_NONE to the label enum that is used for proto3 fields. When a fields has this label, the quantifier is not consulted and instead the field is packed/unpacked depending on whether it has a value different from NULL/0.

    The patch changes the generated-code test to proto3 as a quick test, clearly this should not be part of the final patch.

    The patch also slightly refactors packing/unpacking optional fields so that the internal functions take a boolean istead of a pointer to a boolean: casting in the caller seems cleaner to me... this part can be dropped or split out in its own preliminary patch.

    opened by pbor 24
  • proto3 and nested submessages

    proto3 and nested submessages

    I think I found a bug using the current code with proto3 syntax. I'm using a protobuf with two layers of nested repeated submessages, in other words I have a subsubmessage. I can populate the fields just fine, but packing the buffer turns all the subsubmessage values to 0. The other fields in the submessage are sent fine. Switching back to proto2 fixes the issue.

    opened by btmit 14
  • unable to get ProtobufCFieldDescriptor for oneof message

    unable to get ProtobufCFieldDescriptor for oneof message

    if I have a message with one of like

    message Wrapper { oneof msg { MsgA msg_a = 1; MsgB msg_b = 2; } }

    How can I get the offset at which I can write my message A or B?

    In other words, if I do

    desc = protobuf_c_message_descriptor_get_field_by_name(descriptor, "msg_a"); I can get desc->offset

    But this requires knowing that the field is called "msg_a". Is there any way to obtain the same offset only knowing that the oneof is called "msg"?

    Basically the use case I have is that I have many wrapper messages that contain "oneof msg", but the actual fields inside the oneof change. I would like to be able to write a generic function that gives me the "unwrapped" message

    opened by pbor 14
  • fix compilation errors on Visual Studio

    fix compilation errors on Visual Studio

    I have separated the Visual Studio build errors from cmake (https://github.com/protobuf-c/protobuf-c/pull/155).

    Here is the patch that allows compilation on Windows. It fixes 3 different (but simple) errors with following reasons:

    • use _inline for non-c99 msvc compiler
    • use char* instead of void* in pointer shifting
    • on non-gnuc family compilers DEPRECATED macros was undefined

    (see also https://github.com/protobuf-c/protobuf-c/issues/168)

    opened by alex85k 14
  • unpack to stack

    unpack to stack

    It'd be nice to have a way to unpack to a struct on the stack, rather than require an allocator. This might not be practical for protos with strings or similar, but should be fine for number-only data.

    wontfix protobuf-c 
    opened by luke-jr 13
  • Invalid namespace

    Invalid namespace

    google::protobuf::message::Reflaction is not exist namespace.

    I try to ubuntu build. But namespace is invalid, so can't build.

    I changed correct namespace

    opened by storyun7 12
  • Install failed

    Install failed

    There were new changes in https://github.com/google/protobuf.git. Because of that protobuf-c install is getting failed. Error faced as below:

    checking google/protobuf/compiler/command_line_interface.h usability... no checking google/protobuf/compiler/command_line_interface.h presence... no checking for google/protobuf/compiler/command_line_interface.h... no ERROR: Installing protobuf-c library. Please check env_protobuf.log

    opened by sanjayku 12
  • proto3: make strings default to

    proto3: make strings default to "" instead of NULL

    The spec talks about "empty string" and other languages like C# return a zero length string and not null. This is useful because when moving from proto2's "required string" to a proto3's plain string we will be guaranteed that we never get a null pointer. The tradeoff is adding a special case to the library but avoiding a lot of null checks in the calling code. The current code is already special casing "" in pack_string.

    opened by pbor 12
  • compile using casues std:bad_Alloc error

    compile using casues std:bad_Alloc error

    step1:

    ./configure CFLAGS=-m64 CXXFLAGS=-m64 LDFLAGS=-m64^C

    step 2: make

    Get error during make stage: GEN t/test.pb-c.c terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc /bin/sh: line 1: 4337 Aborted (core dumped) ./protoc-c/protoc-c -I. --c_out=. ./t/test.proto

    GEN t/test.pb-c.c terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc /bin/sh: line 1: 4337 Aborted (core dumped) ./protoc-c/protoc-c -I. --c_out=. ./t/test.proto

    GEN t/test.pb-c.c terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc /bin/sh: line 1: 4337 Aborted (core dumped) ./protoc-c/protoc-c -I. --c_out=. ./t/test.proto

    unreproducible 
    opened by edwardt 12
  • text format is support...

    text format is support...

    I like to use protobufs as a config file format so the ability to parse text format protobufs would be nice. It's also nice to be able to dump them to text.

    I've been using the ProtobufCMessageDescriptor and ProtobufCFieldDescriptor types and they seem like they'll be useful in that and I have the basics of a text format generator now. A parser should be relatively easy.

    Would there be any interest in this code for libprotobuf-c? It would really add a lot of bulk to the library as the linker will just discard it if it's not used. Or I could make a different target for it - say libprotobuf-c-text.

    Thoughts?

    opened by lyda 11
  • make error on Ubuntu 14.0LTS

    make error on Ubuntu 14.0LTS

    I cloned protobuf-c from git master and trying to make it run but i Have following errors: image

    After configuring the output looks like this :

    image

    I am wondering whats wrong there.

    opened by saumilagg 10
  • Windows cmake build: mismatch detected for '_ITERATOR_DEBUG_LEVEL'

    Windows cmake build: mismatch detected for '_ITERATOR_DEBUG_LEVEL'

    While building protobuf-c using the provided cmake file I encountered the following linker error (the first of many similar errors):

    libprotoc.lib(code_generator.cc.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't
    match value '2' in c_bytes_field.obj [C:\Users\brian\Documents\protobuf-c\build-cmake\build\protoc-gen-c.vcxproj]
    libprotoc.lib(code_generator.cc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease'
    doesn't match value 'MTd_StaticDebug' in c_bytes_field.obj [C:\Users\brian\Documents\protobuf-c\build-cmake\build\proto
    c-gen-c.vcxproj]
    

    Here are the steps I followed and the build output. I cloned protobuf-c from master.

    C:\Users\brian\Documents\protobuf-c\build-cmake> mkdir build && cd build
    C:\Users\brian\Documents\protobuf-c\build-cmake\build> cmake ..
    -- Building for: Visual Studio 17 2022
    -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
    -- The C compiler identification is MSVC 19.31.31104.0
    -- The CXX compiler identification is MSVC 19.31.31104.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    C:/Users/brian/Documents/protobuf-c
    -- Found Protobuf: C:/Users/brian/Documents/protobuf/lib/libprotobuf.lib (found version "3.21.12")
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/Users/brian/Documents/protobuf-c/build-cmake/build
    
    C:\Users\brian\Documents\protobuf-c\build-cmake\build> cmake --build .
    

    It's a pretty basic usage of cmake, so I'm assuming it's a bug. Compilation works fine but linking doesn't. Here's what else I tried:

    cmake -DCMAKE_BUILD_TYPE=Release .. cmake -G "Visual Studio 15 2017 Win64" .. to select the Visual Studio 2017 tools.

    opened by brianheilig 0
  • can i genarate C struct which has been init ?

    can i genarate C struct which has been init ?

    now the proto convert to <xxxpb-c.h> has a macro like this : #define P_DETECT__IFIT_FLOWINFO__INIT \ { PROTOBUF_C_MESSAGE_INIT (&p_detect__ifit_flowinfo__descriptor) \ , NULL, 0, NULL, 0, 0, 0, 0, 0, 0, (char *)protobuf_c_empty_string, 0 } however i don't want the pointer filed initialized as NULL point, is there any solution?

    opened by Nononoa-Zoro 0
  • Does not compile under Debian 11.5

    Does not compile under Debian 11.5

    Hi,

    the current git repos does not compile under Debian 11.5

    uname 
      5.10.0-19-amd64
    g++ --version
      g++ (Debian 10.2.1-6) 10.2.1
    
    git clone https://github.com/protobuf-c/protobuf-c.git
    cd protobuf-c
    ./autogen.sh
    ./configure
        protobuf-c 1.4.1
    
            CC:                     gcc
            CFLAGS:                 -g -O2
            CXX:                    g++ -std=c++11
            CXXFLAGS:               -g -O2
            LDFLAGS:                
            LIBS:                   
    
            prefix:                 /usr/local
            sysconfdir:             ${prefix}/etc
            libdir:                 ${exec_prefix}/lib
            includedir:             ${prefix}/include
            pkgconfigdir:           ${libdir}/pkgconfig
    
            bigendian:              no
            protobuf version:       libprotoc 21.9
            
    make
      GEN      protobuf-c/protobuf-c.pb.cc
      CXX      protoc-c/protoc_gen_c-c_bytes_field.o
    In file included from ./protoc-c/c_field.h:67,
                     from ./protoc-c/c_bytes_field.h:68,
                     from protoc-c/c_bytes_field.cc:63:
    /usr/local/include/google/protobuf/stubs/common.h:63:2: error: #error "C++ versions less than C++14 are not supported."
       63 | #error "C++ versions less than C++14 are not supported."
          |  ^~~~~
    In file included from ./protoc-c/c_bytes_field.h:68,
                     from protoc-c/c_bytes_field.cc:63:
    ./protoc-c/c_field.h:108:3: error: ISO C++ forbids declaration of ‘GOOGLE_DISALLOW_EVIL_CONSTRUCTORS’ with no type [-fpermissive]
      108 |   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
          |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ....
    

    Editing Makefile and change to

    CXX = g++ -std=c++14
    CXXCPP = g++ -E -std=c++14
    

    does not help:

    make
      CXX      protoc-c/protoc_gen_c-c_bytes_field.o
    In file included from ./protoc-c/c_bytes_field.h:68,
                     from protoc-c/c_bytes_field.cc:63:
    ./protoc-c/c_field.h:108:3: error: ISO C++ forbids declaration of ‘GOOGLE_DISALLOW_EVIL_CONSTRUCTORS’ with no type [-fpermissive]
      108 |   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
          |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ./protoc-c/c_field.h:108:37: error: cannot declare parameter to be of abstract type ‘google::protobuf::compiler::c::FieldGenerator’
      108 |   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
    ....
    

    How to resolve?

    Thanks Marcel

    opened by marcelr 1
  • Add CIFuzz GitHub action

    Add CIFuzz GitHub action

    Add CIFuzz workflow action to have fuzzers build and run on each PR.

    This is a service offered by OSS-Fuzz where protobuf-c already runs (https://github.com/google/oss-fuzz/tree/master/projects/protobuf-c). CIFuzz can help detect regressions and catch fuzzing build issues early, and has a variety of features (see the URL above). In the current PR the fuzzers gets build on a pull request and will run for 120 seconds.

    opened by DavidKorczynski 0
  • Q: which C (not C++) grpc library do you use with protobuf-c?

    Q: which C (not C++) grpc library do you use with protobuf-c?

    Hi,

    Maybe a silly question: but which C (not C++) grpc library do you use with protobuf-c?

    (I searched a bit, but didn't find any good / active maintained C (not C++) grpc library on the web).

    Or may I use a Rust (or some other language's) grpc library with protobuf-c? has anyone tried that? any tutorials?

    Thanks.

    opened by mw66 0
Releases(v1.4.1)
Owner
null
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Protocol Buffers 57.5k Dec 29, 2022
Google Protocol Buffers tools (C code generator).

About Google Protocol Buffers tools in Python 3.6+. C source code generator. Rust source code generator ( ?? ?? ?? under construction ?? ?? ?? ). prot

Erik Moqvist 51 Nov 29, 2022
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Protocol Buffers 57.6k Jan 3, 2023
MessagePack implementation for C and C++ / msgpack.org[C/C++]

msgpack for C/C++ It's like JSON but smaller and faster. Overview MessagePack is an efficient binary serialization format, which lets you exchange dat

MessagePack 2.6k Dec 31, 2022
a small protobuf implementation in C

μpb - a small protobuf implementation in C Platform Build Status macOS ubuntu μpb (often written 'upb') is a small protobuf implementation written in

Protocol Buffers 1.4k Jan 6, 2023
An implementation of the MessagePack serialization format in C / msgpack.org[C]

CMP CMP is a C implementation of the MessagePack serialization format. It currently implements version 5 of the MessagePack Spec. CMP's goal is to be

Charlie Gunyon 290 Dec 31, 2022
Base64 Encoding implementation in C Programming Language

cb64 Base64 Encoding implementation in C Programming Language Spec: https://datatracker.ietf.org/doc/html/rfc4648#page-5 Header only So just copy cb64

Telkom DEV 1 Dec 6, 2022
static_vector implementation in terms of std::vector. No need to implement each function again and again ;)

static_vector static_vector implementation in terms of std::vector. No need to implement each function again and again ;) The usage is basically the s

Alex 6 Oct 29, 2022
Protocol Buffers with small code size

Nanopb - Protocol Buffers for Embedded Systems Nanopb is a small code-size Protocol Buffers implementation in ansi C. It is especially suitable for us

null 3.3k Dec 31, 2022
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Protocol Buffers 57.5k Dec 29, 2022
Google Protocol Buffers tools (C code generator).

About Google Protocol Buffers tools in Python 3.6+. C source code generator. Rust source code generator ( ?? ?? ?? under construction ?? ?? ?? ). prot

Erik Moqvist 51 Nov 29, 2022
A protocol buffers library for C

PBC PBC is a google protocol buffers library for C without code generation. Quick Example package tutorial; message Person { required string name =

云风 1.6k Dec 28, 2022
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Protocol Buffers 57.6k Jan 3, 2023
Simple and fast C library implementing a thread-safe API to manage hash-tables, linked lists, lock-free ring buffers and queues

libhl C library implementing a set of APIs to efficiently manage some basic data structures such as : hashtables, linked lists, queues, trees, ringbuf

Andrea Guzzo 392 Dec 3, 2022
CUDA Custom Buffers and example blocks

gr-cuda CUDA Support for GNU Radio using the custom buffer changes introduced in GR 3.10. Custom buffers for CUDA-enabled hardware are provided that c

GNU Radio 5 Aug 17, 2022
An implementation of the Mobile Adapter GB protocol, to connect to custom game servers.

libmobile Library that implements the Mobile Adapter GB protocol, in a way that should be easy to integrate into a plethora of different emulators/har

REON Team 10 Nov 5, 2022
The implementation of the Domino Network protocol based program.

spacex The implementation of the Domino distributed storage network protocol for the Smart Ecology. With a fully featured and well documented that pro

DominoNetwork 4 Sep 26, 2021
Simple HTTP protocol implementation in C

C-SimpleHTTP A simple HTTP protocol implementation in C How do i run it? First of all, this project is made in c, then you will need GCC Compiler. The

Spar 8 May 25, 2022
xeus-wren is a Jupyter kernel for wren based on the native implementation of the Jupyter protocol xeus.

xeus-wren is a Jupyter kernel for wren based on the native implementation of the Jupyter protocol xeus. Installation xeus-wren has not been packaged f

Thorsten Beier 4 Mar 9, 2022