Google Protocol Buffer 在 STM32 平台上的移植

前言

因为项目需要,我需要在 STM32 平台上使用 Google 出品的 Protocol Buffer 打包数据发送到服务器,但是官方的
protoc 翻译器不支持生成c语言版本的代码,无奈只好去 github 搜一下轮子,搜到了
protobuf-c 这个轮子,并成功将其翻译器在
win10 平台上编译,详情请见下文。
下载请戳我的 github repo
文件夹结构如下:
Google Protocol Buffer 在 STM32 平台上的移植

怎么使用 gen.exe?

gen.exe是我写的一个懒人工具。

  1. 打开命令行.
  2. 执行 gen --help.
  3. 然后你就知道怎么用了.

怎么使用 Keil5 编译?

  1. protobuf-c 文件夹添加进你的 keil 工程.
  2. 使用 gen.exe 翻译 proto 文件,生成相应的 c 文件 和 h 文件, 把它们也添加到 keil 工程.
  3. 参考 test 文件夹里面的文件,写你的应用.
  4. 别勾选 MicroLib.

How to compile protoc.exe?

  1. Install MSYS2.

  2. New a folder, for example, pbc.

  3. Enter pbc, new a file named “PKGBUILD”.

  4. Fill the file with the code below.

    # Maintainer: Alexey Pavlov <[email protected]>
    
    _realname=protobuf-c
    pkgbase=mingw-w64-${_realname}
    pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
    pkgver=1.3.1
    pkgrel=1
    pkgdesc="Protocol Buffers implementation in C (mingw-w64)"
    arch=('any')
    url="https://github.com/protobuf-c/protobuf-c"
    license=('BSD')
    depends=("${MINGW_PACKAGE_PREFIX}-protobuf")
    makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
    options=('staticlibs' 'strip')
    source=("$url/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz")
    sha256sums=('51472d3a191d6d7b425e32b612e477c06f73fe23e07f6a6a839b11808e9d2267')
    
    prepare() {
      cd "${srcdir}/${_realname}-${pkgver}"
      autoreconf -fiv
    }
    
    build() {
      [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
      mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
    
      ../${_realname}-${pkgver}/configure \
          --prefix=${MINGW_PREFIX} \
          --build=${MINGW_CHOST} \
          --host=${MINGW_CHOST}
      make
    }
    
    check() {
      cd "${srcdir}/build-${MINGW_CHOST}"
      make check || true
    }
    
    package() {
      cd "${srcdir}/build-${MINGW_CHOST}"
      make DESTDIR="${pkgdir}" install
      install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
    }
    
  5. Open a terminal in this folder, type MINGW_INSTALLS=mingw64 makepkg-mingw -sLf.

    Google Protocol Buffer 在 STM32 平台上的移植

  6. Wait for compiling done.

    Google Protocol Buffer 在 STM32 平台上的移植

  7. Install the lib you have just compiled.

    type pacman -U mingw-w64-x86_64-protobuf-c*.pkg.tar.xz, enter.

    Google Protocol Buffer 在 STM32 平台上的移植

  8. Copy your protoc.exe from <your MSYS2 path>/mingw64/bin.

  9. Do what you want.

    Google Protocol Buffer 在 STM32 平台上的移植