protoc 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

protoc 命令安装:


  1. -bash/zsh: protoc command not found
  2.  
  3. #Debian
  4. apt-get install protobuf-compiler
  5.  
  6. #Ubuntu
  7. apt-get install protobuf-compiler
  8.  
  9. #Alpine
  10. apk add protobuf
  11.  
  12. #Arch Linux
  13. pacman -S protobuf
  14.  
  15. #Kali Linux
  16. apt-get install protobuf-compiler
  17.  
  18. #CentOS
  19. yum install protobuf-compiler
  20.  
  21. #Fedora
  22. dnf install protobuf-compiler
  23.  
  24. #OS X
  25. brew install protobuf
  26.  
  27. #Raspbian
  28. apt-get install protobuf-compiler
  29.  
  30. #Docker
  31. docker run cmd.cat/protoc protoc
  32.  
  33. #Github 上的版本发布地址
  34. https://github.com/protocolbuffers/protobuf/releases

protoc 命令补充说明:


protoc 命令,用于解析 Google Protobuf.proto 文件,并以指定的语言(如 Java, C#, PHP, Python...)生成输出。

protoc 命令语法:


  1. protoc [OPTION] PROTO_FILES

protoc 命令选项:


  1. Parse PROTO_FILES and generate output based on the options given:
  2. -IPATH, --proto_path=PATH Specify the directory in which to search for
  3. imports. May be specified multiple times;
  4. directories will be searched in order. If not
  5. given, the current working directory is used.
  6. If not found in any of the these directories,
  7. the --descriptor_set_in descriptors will be
  8. checked for required proto file.
  9. --version Show version info and exit.
  10. -h, --help Show this text and exit.
  11. --encode=MESSAGE_TYPE Read a text-format message of the given type
  12. from standard input and write it in binary
  13. to standard output. The message type must
  14. be defined in PROTO_FILES or their imports.
  15. --decode=MESSAGE_TYPE Read a binary message of the given type from
  16. standard input and write it in text format
  17. to standard output. The message type must
  18. be defined in PROTO_FILES or their imports.
  19. --decode_raw Read an arbitrary protocol message from
  20. standard input and write the raw tag/value
  21. pairs in text format to standard output. No
  22. PROTO_FILES should be given when using this
  23. flag.
  24. --descriptor_set_in=FILES Specifies a delimited list of FILES
  25. each containing a FileDescriptorSet (a
  26. protocol buffer defined in descriptor.proto).
  27. The FileDescriptor for each of the PROTO_FILES
  28. provided will be loaded from these
  29. FileDescriptorSets. If a FileDescriptor
  30. appears multiple times, the first occurrence
  31. will be used.
  32. -oFILE, Writes a FileDescriptorSet (a protocol buffer,
  33. --descriptor_set_out=FILE defined in descriptor.proto) containing all of
  34. the input files to FILE.
  35. --include_imports When using --descriptor_set_out, also include
  36. all dependencies of the input files in the
  37. set, so that the set is self-contained.
  38. --include_source_info When using --descriptor_set_out, do not strip
  39. SourceCodeInfo from the FileDescriptorProto.
  40. This results in vastly larger descriptors that
  41. include information about the original
  42. location of each decl in the source file as
  43. well as surrounding comments.
  44. --dependency_out=FILE Write a dependency output file in the format
  45. expected by make. This writes the transitive
  46. set of input file paths to FILE
  47. --error_format=FORMAT Set the format in which to print errors.
  48. FORMAT may be 'gcc' (the default) or 'msvs'
  49. (Microsoft Visual Studio format).
  50. --print_free_field_numbers Print the free field numbers of the messages
  51. defined in the given proto files. Groups share
  52. the same field number space with the parent
  53. message. Extension ranges are counted as
  54. occupied fields numbers.
  55.  
  56. --plugin=EXECUTABLE Specifies a plugin executable to use.
  57. Normally, protoc searches the PATH for
  58. plugins, but you may specify additional
  59. executables not in the path using this flag.
  60. Additionally, EXECUTABLE may be of the form
  61. NAME=PATH, in which case the given plugin name
  62. is mapped to the given executable even if
  63. the executable's own name differs.
  64. --cpp_out=OUT_DIR Generate C++ header and source.
  65. --csharp_out=OUT_DIR Generate C# source file.
  66. --java_out=OUT_DIR Generate Java source file.
  67. --js_out=OUT_DIR Generate JavaScript source.
  68. --objc_out=OUT_DIR Generate Objective C header and source.
  69. --php_out=OUT_DIR Generate PHP source file.
  70. --python_out=OUT_DIR Generate Python source file.
  71. --ruby_out=OUT_DIR Generate Ruby source file.
  72. @<filename> Read options and filenames from file. If a
  73. relative file path is specified, the file
  74. will be searched in the working directory.
  75. The --proto_path option will not affect how
  76. this argument file is searched. Content of
  77. the file will be expanded in the position of
  78. @<filename> as in the argument list. Note
  79. that shell expansion is not applied to the
  80. content of the file (i.e., you cannot use
  81. quotes, wildcards, escapes, commands, etc.).
  82. Each line corresponds to a single argument,
  83. even if it contains spaces.

protoc 命令参数:


  1. proto 描述文件及输出目录

protoc 命令实例:


protoc 根据 proto 描述文件,生成不同语言的类代码:

  1. #Python code from a `.proto` file:
  2. protoc --python_out=path/to/output_directory input_file.proto
  3. #Java code from a `.proto` file that imports other `.proto` files:
  4. protoc --java_out=path/to/output_directory --proto_path=path/to/import_search_path input_file.proto
  5.  
  6. #Generate code for multiple languages:
  7. protoc --csharp_out=path/to/c#_output_directory --js_out=path/to/js_output_directory input_file.proto

Linux 下安装 protobuf 2.X 及在 PHP 上的应用


下载 & 解压 & 进入源码目录


当前下载 protobuf 2 的最后一个版本

  1. wget https://github.com/google/protobuf/archive/v2.6.1.zip
  2.  
  3. unzip v2.6.1.zip -d ./
  4.  
  5. cd protobuf-2.6.1
请根据自己的需求下载对应的版本,本文为 protobuf 2.proto 文件中 syntax = "proto2"; 开头的

安装依赖


  1. sudo apt-get install autoconf automake libtool
其他非 Debian 发行版的机器安装对应的依赖扩展即可

生成 configure 文件


  1. ./autogen.sh
  2.  
  3. 如果无法连接 Google 的网站则修改:
  4. curl http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2 | tar jx
  5.  
  6. mv gtest-1.5.0 gtest
  7.  
  8. 为如下:
  9. wget https://github.com/google/googletest/archive/release-1.5.0.tar.gz
  10.  
  11. tar xzvf release-1.5.0.tar.gz
  12. mv googletest-release-1.5.0 gtest
也可由手动删除 Google 的连接,直接下载文件并解压

进行 configure 及安装


  1. ./configure -h #查看帮助
  2.  
  3. ./configure prefix=/Data/apps/protoc-2.6.1 #指定安装目录
  4.  
  5. make && make install
建立命令软连接:ln -s /Data/apps/protoc-2.6.1/bin/protoc /usr/local/bin/protoc,然后可以直接使用命令 protoc 了。

protoc 在 PHP 中的应用:


下载 allegro/php-protobuf


  1. #Github 版本发布页面
  2. https://github.com/allegro/php-protobuf/releases
  3.  
  4. #下载最新发布的版本(2019.01.07 版本为 v0.12.4)
  5. wget https://github.com/allegro/php-protobuf/archive/v0.12.4.tar.gz
  6.  
  7. #解压到当前目录
  8. unzip v0.12.4.zip -d ./

安装 PHP 扩展


  1. cd php-protobuf-0.12.4
  2.  
  3. phpize
  4.  
  5. ./configure --with-php-config=/Data/apps/php/bin/php-config #可以指定之前的编译配置
  6.  
  7. make
  8. make install
  9.  
  10. 配置 php.ini
  11. extension=protobuf.so
执行 php --ri protobuf 输出 protobuf 扩展信息即为正常,如:
  1. $ php --ri protobuf
  2.  
  3. protobuf
  4.  
  5. Version => 0.12.3

在源码目录安装 allegro/php-protobuf 依赖的 PHP 类库:


composer install 安装即可

使用 protobuf 生成 .php 文件


  1. #Baidu 为当前目录,根据百度 proto 描述文件,生成百度聚屏的 PHP 类文件
  2. ./protoc --plugin=protoc-gen-allegrophp=/Data/tools/php-protobuf/protoc-gen-php.php --allegrophp_out=Baidu ts_ui_api_v20191205.proto

修改完 php.ini 后,执行 /etc/init.d/php-fpm restart,PHP 查看是否已加载的所有的 so

  1. $ php -m
  2.  
  3. [PHP Modules]
  4. bcmath
  5. bz2
  6. calendar
  7. Core
  8. ctype
  9. curl
  10. date
  11. dba
  12. dom
  13. exif
  14. fileinfo
  15. filter
  16. ftp
  17. gd
  18. gettext
  19. gmp
  20. hash
  21. iconv
  22. json
  23. libxml
  24. mbstring
  25. memcache
  26. memcached
  27. mysqli
  28. mysqlnd
  29. openssl
  30. pcre
  31. PDO
  32. pdo_mysql
  33. pdo_sqlite
  34. Phar
  35. posix
  36. protobuf
  37. readline
  38. redis
  39. Reflection
  40. session
  41. shmop
  42. SimpleXML
  43. soap
  44. sockets
  45. SPL
  46. sqlite3
  47. standard
  48. sysvmsg
  49. sysvsem
  50. sysvshm
  51. tokenizer
  52. wddx
  53. xml
  54. xmlreader
  55. xmlrpc
  56. xmlwriter
  57. xsl
  58. Zend OPcache
  59. zip
  60. zlib
  61.  
  62. [Zend Modules]
  63. Zend OPcache

protoc 命令扩展阅读:




protoc 命令评论