(.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory

看这个就应该知道是没有找到头文件,那么可以使用g++ 的-I 参数:
-I/usr/local/lib/protobuf/include来命令g++在/usr/local/lib/protobuf下查找头文件
以上/usr/local/lib/protobuf/是我的protobuf的安装地址,请替换成你的

)
 
 
1 在网站 http://code.google.com/p/protobuf/downloads/list上可以下载 Protobuf 的源代码。然后解压编译安装便可以使用它了。
安装步骤如下所示:
 tar -xzf protobuf-2.1.0.tar.gz 
 cd protobuf-2.1.0 
 ./configure --prefix=/usr/local/protobuf
 make 
 make check 
 make install 
 
 2 > sudo vim /etc/profile
 添加
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
保存执行
source /etc/profile
同时 在~/.profile中添加上面两行代码,否则会出现登录用户找不到protoc命令
3 > 配置动态链接库路径
sudo vim /etc/ld.so.conf
插入:
/usr/local/protobuf/lib
4 > su  #root 权限
ldconfig

5> 写消息文件:msg.proto

  1. package lm;
  2. message helloworld
  3. {
  4. required int32     id = 1;  // ID
  5. required string    str = 2;  // str
  6. optional int32     opt = 3;  //optional field
  7. }
将消息文件msg.proto映射成cpp文件
protoc -I=. --cpp_out=. msg.proto
可以看到生成了
msg.pb.h 和msg.pb.cc
6> 写序列化消息的进程
write.cc
  1. #include "msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. int main(void)
  6. {
  7. lm::helloworld msg1;
  8. msg1.set_id(101);
  9. msg1.set_str("hello");
  10. fstream output("./log", ios::out | ios::trunc | ios::binary);
  11. if (!msg1.SerializeToOstream(&output)) {
  12. cerr << "Failed to write msg." << endl;
  13. return -1;
  14. }
  15. return 0;
  16. }
编译 write.cc 
 g++  msg.pb.cc write.cc -o write  `pkg-config --cflags --libs protobuf` -lpthread
 
执行write 
./write, 可以看到生成了log文件
7> 写反序列化的进程
reader.cc

  1. #include "msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. void ListMsg(const lm::helloworld & msg) {
  6. cout << msg.id() << endl;
  7. cout << msg.str() << endl;
  8. }
  9. int main(int argc, char* argv[]) {
  10. lm::helloworld msg1;
  11. {
  12. fstream input("./log", ios::in | ios::binary);
  13. if (!msg1.ParseFromIstream(&input)) {
  14. cerr << "Failed to parse address book." << endl;
  15. return -1;
  16. }
  17. }
  18. ListMsg(msg1);
  19. }
编译:g++  msg.pb.cc reader.cc -o reader  `pkg-config --cflags --libs protobuf` -lpthread
执行./reader 输出 :
101
hello

8> 写Makefile文件

    1. all: write reader
    2. clean:
    3. rm -f write reader msg.*.cc msg.*.h *.o  log
    4. proto_msg:
    5. protoc --cpp_out=. msg.proto
    6. write: msg.pb.cc write.cc
    7. g++  msg.pb.cc write.cc -o write  `pkg-config --cflags --libs protobuf`
    8. reader: msg.pb.cc reader.cc
    9. g++  msg.pb.cc reader.cc -o reader  `pkg-config --cflags --libs protobuf`

linux下安装protobuf教程+示例(详细)的更多相关文章

  1. Linux下安装GB2312的示例

    Linux下安装GB2312的示例 Step 1: 到Linux字符集的安装包目录下  [cd /usr/share/i18n/charmaps] Step 2: 解压该目录下的GB2312.gz   ...

  2. Linux下安装mysql教程

    Linux下安装mysql  MySQL官网:https://dev.mysql.com/downloads/mysql/ 到mysql官网下载mysql编译好的二进制安装包,在下载页面Select ...

  3. Linux下安装protobuf并实现简单的客户端服务器端通信

    http://code.google.com/p/protobuf/downloads/list上可以下载Protobuf的源代码. 安装步骤如下所示: 1>tar -xzf protobuf- ...

  4. linux下 安装mysql教程

    安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...

  5. Linux下安装mysql-5.7.28详细步骤

    一.下载Mysql 下载地址:https://downloads.mysql.com/archives/community/ 二.环境配置 检测系统是否自带Mysql # rmp -qa|grep m ...

  6. linux下安装protobuf及cmake编译

    一.protobuf 安装 protobuf版本:2.6.1 下载地址:https://github.com/google/protobuf/archive/v2.6.1.zip 解压之后进入目录 修 ...

  7. linux下安装mysql5.7.25详细教程

    前言 最近项目上线,开始给用户测试了.搞下来好多台服务器,自然要装一个mysql的服务器.想想广大博友应该都会遇到如何装mysql的问题,就此分享,给大家一个安装指南.供大家以后安装的时候,提高效率, ...

  8. Linux下安装mysql(示例mysql5.6安装)

    1.首先检查你的linux上是否已经安装了mysql rpm -qa|grep mysql 2.如果mysql的版本不是想要的版本.需要把mysql卸载 yum remove mysql mysql- ...

  9. Linux下安装Nginx教程

    什么是Nginx? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,在高连接并发的情况下N ...

随机推荐

  1. java 中的原始类型与原始封装类型

    Java   提供两种不同的类型:引用类型和原始类型(或内置类型).比如:Int是java的原始数据类型,Integer是java为int提供的封装类.Java为每个原始类型提供了封装类,常见的原始与 ...

  2. mysql数据导入

    1.windows解压 2.修改文件名,例如a.txt 3.rz 导入到 linux \data\pcode sudo su -cd /data/pcode/rm -rf *.txt 4.合并到一个文 ...

  3. 使用POI导入Excel异常Cannot get a text value from a numeric cell 解决

    POI操作Excel时因为Excel数据Cell有不同的类型,会出现Cannot get a text value from a numeric cell的异常错误. 异常原因:Excel数据Cell ...

  4. MVC ueditor的使用(实现上传图片功能)

    之前使用ckeditor不能实现上传图片功能,只要是我不知道怎么使用啦o( ̄ε ̄*),然后就换了ueditor~~,可以实现上传图片功能啦~\(≧▽≦)/~~ 下面是我的步骤:去官网下载最新版uedi ...

  5. .net框架版本说明

    .NET框架 1.0..NET框架 1.1..NET框架 2.0..NET框架 3.0..NET框架 3.5..NET框架 4.00 .netframework3.0Windows Presentat ...

  6. 检查字符串长度 检查字符串是否为空 用正则表达式验证出版物的ISBN号 用正则表达式验证邮证编码 验证字符串中是否含有汉字

    <?php /** * 常用的正则表达式来验证信息.如:网址 邮箱 手机号等 */ class check { /** * 正则表达式验证email格式 * * @param string $s ...

  7. (笔记)angular 包含关系的controller参数传递

  8. ArrayList总结

    ArrayList 1.extends AbstractList 实现List<E>->Collection<e>->Iterable,RandomAccess,S ...

  9. Android IOS WebRTC 音视频开发总结(十六)-- 音频设备操作之opensl与jni

    本节主要分享视频通话中android和ios上操作音频设备的方式,如调解音量大小,启用扬声器(本系列文章转载请说明出处,博客园RTC.Blacker). 先看看webrtc中处理音频设备代码的目录结构 ...

  10. javaSE第十九天

    第十九天    227 1:异常(理解)    227 (1) 定义    227 a)异常的引入    227 (2)异常的体系    228 (3)异常的处理:    229 A:JVM的默认处理 ...