天天看點

google protobuf在ubuntu上安裝使用步驟

1、從github下載下傳protobuff源碼。 

     https://github.com/google/protobuf

2、安裝依賴項:

  * autoconf

  * automake

  * libtool

  * curl (used to download gmock)

執行下面即可:

  $ sudo apt-get install autoconf automake libtool curl

3、産生安裝腳本:

     $ ./autogen.sh    ##注意:此處需要翻牆才能成功

4、安裝:

    $ cd protobuf-master      $ ./configure --prefix=/usr/local/protobuf   ##這裡是定義安裝的位置     $ make

    $ make check

    $ sudo make install

    $ sudo ldconfig # refresh shared library cache.

5、配置動态連結庫路徑

     sudo vim /etc/ld.so.conf

      插入:

     /usr/local/protobuf/lib變量。

    sudo vim /etc/profile  添加    export PATH=$PATH:/usr/local/protobuf/bin/    export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/    儲存執行    source /etc/profile

6、如果要交叉編譯的話,比如我要編譯hi3518上面用的庫時,./configure的時候這樣設定      ./configure --build=i686-pc-linux  --host=arm-hisiv100nptl-linux --with-protoc=protoc --disable-shared --prefix=/home/huang/Desktop/thridparty/protobuf/2.5.0/forHaisi

然後make&&make install 就可以了

寫消息檔案:msg.proto

package lm; 
message helloworld 
{ 
    required int32     id = 1;  // ID   
    required string    str = 2;  // str  
    optional int32     opt = 3;  //optional field 
}
           

将消息檔案msg.proto映射成cpp檔案 protoc -I=. --cpp_out=. msg.proto 可以看到生成了 msg.pb.h 和msg.pb.cc

----------------------------------結束------------------------------------------    

繼續閱讀