Linux Eclipse 運行Protobuf

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   strong   檔案   io   

安裝環境Ubuntu 14.04 64 bit

 

安裝過程分為三步

1. Linux下安裝Protobuf

2. Eclipse下安裝protobuf.dt外掛程式

3. Eclipse下配置動態連結程式庫並運行demo

 

第一步 Linux下安裝protobuf

到官網下載protobuf,目前的最新版本是2.5.0,也是超過1年沒有更新了

解壓,進入檔案夾內,依次運行下面的命令

./config

make

make check

sudo make install(這一步需要管理員權限,否則會報錯)

修改動態連結程式庫

sudo su - 以管理員身份執行下面的命令

在 vim ld.so.conf 加入 /usr/local/lib儲存退出

執行ldconfig

protoc --version 即可看到版本號碼

這裡注意,首先make install命令需要建立檔案夾,因此要管理員權限;其次,直接su ldconfig在我機器上會報錯,我就直接sudo su -;最後,我直接將./config安裝到預設的檔案夾下(/usr/local/lib)下了,因此不需要配置環境變數

 

第二步 Eclipse下安裝protobuf.dt外掛程式

連結: https://code.google.com/p/protobuf-dt/wiki/Installing

實際上需要裝兩個外掛程式,一個是xtext, 另外是protobuf-dt

直接到eclipse的market去搜就行了

我先裝的xtext2.6,又裝了protobuf-dt,結果安裝失敗了,又裝了一遍xtext才成功,不知道為什麼。

安裝完畢後需要在preference做些配置,預設情況下protobuf是不啟用的

配置可參考連結: https://code.google.com/p/protobuf-dt/wiki/IntegrationWithProtoc

完成第二步後,可以嘗試在一個project鐘寫xxx.proto檔案,你會看到文法高亮,儲存後,會自動產生src-gen檔案夾,檔案夾下有xxx.pb.h和xxx.pb.cc檔案

 

第三步 配置動態連結程式庫
#include "msg.pb.h"#include <fstream>#include <iostream>using namespace std;int main(void) {    lm::helloworld msg1;    msg1.set_id(101);    msg1.set_str("hello");    fstream output("./log", ios::out | ios::trunc | ios::binary);    if(!msg1.SerializeToOstream(&output)) {        cerr << "Failed to write msg." << endl;        return -1;    }    return 0;}

寫了一個demo,eclipse下編譯失敗。

但在命令列下 g++ msg.pb.cc writer.cpp -o writer -lprotobuf -lpthread就能編譯成功

 

右擊project,依次點擊preference, C/C++ Buid, Settting, 在Setting下,找到GCC C++ Linker, Libraries, 在Libraries(-l)中添加兩個條目,分別是protobuf和pthread

 

第三步卡了我很久,其實這個問題在我以前用boost的時候就碰到過,當初的情況好些,畢竟boost用的人比較多,最終也是靠尋找以前做的記錄才搞好。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.