QuarkXPress gps wpf express dart split nuget Seajs Minjs alertifyjs vue论坛 sql server 视频教程 jquery多个元素绑定同一个事件 java两个数组合并 mysql删除一列 map删除指定元素 ab软启动器 kafka学习 css面试题 java使用redis mysql时间戳转日期 python功能 python文件 python入门例子 python安装模块 java集合 java数据库 java开发接口 java获取ip地址 java安装与配置 linux格式化命令 asp建站系统 不寻常的指南针 win10长期服务版 微信python退出程序 cg模宝 msdev lseek函数 comsol软件下载 图片轮播代码
当前位置: 首页 > 学习教程  > 编程语言

ubuntu下protobuf使用案例[命令行->cmake->VS code]

2020/8/11 20:55:22 文章标签:

系统: ubuntu 18.04

一、protobuf安装

sudo apt-get install autoconf automake libtool curl make g++ unzip git
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
protoc --version

若最后输出类似如下信息,说明protobuf安装成功。
在这里插入图片描述

二、命令行使用protobuf

2.1 构建proto文件

mkdir -p CSDN_ws/protobuf/teminal/
cd CSDN_ws/protobuf/teminal/
gedit AddressBook.proto

将以下文本复制到AddressBook.proto,并保存。

package tutorial;

message Person {
    required string name=1;
    required int32 id=2;
    optional string email=3;

    enum PhoneType {
        MOBILE=0;
        HOME=1;
        WORK=2;
    }

    message PhoneNumber {
        required string number=1;
        optional PhoneType type=2 [default=HOME];
    }

    repeated PhoneNumber phone=4;
}

message AddressBook {
    repeated Person person=1;
}

2.2 生成c++头文件

protoc --cpp_out=./ AddressBook.proto
ls

此时发现,目录下多出两个文件:AddressBook.pb.ccAddressBook.pb.h
在这里插入图片描述

2.3 使用protobuf头文件

gedit main.cpp

将以下文本复制到main.cpp,并保存。

#include <iostream>
#include <fstream>
#include <vector>
#include "AddressBook.pb.h"

using namespace std;

// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) 
{
    cout << "Enter person ID number: ";
    int id;
    cin >> id;
    person->set_id(id);
    cin.ignore(256, '\n');

    cout << "Enter name: ";
    getline(cin, *person->mutable_name());

    cout << "Enter email address (blank for none): ";
    string email;
    getline(cin, email);
    if (!email.empty()) 
    {
        person->set_email(email);
    }

    while (true) 
    {
        cout << "Enter a phone number (or leave blank to finish): ";
        string number;
        getline(cin, number);
        if (number.empty()) 
        {
            break;
        }

        tutorial::Person::PhoneNumber* phone_number = person->add_phone();
        phone_number->set_number(number);

        cout << "Is this a mobile, home, or work phone? ";
        string type;
        getline(cin, type);
        if (type == "mobile") 
        {
            phone_number->set_type(tutorial::Person::MOBILE);
        } 
        else if (type == "home") 
        {
            phone_number->set_type(tutorial::Person::HOME);
        }
         else if (type == "work") 
         {
            phone_number->set_type(tutorial::Person::WORK);
        }
        else 
        {
            cout << "Unknown phone type.  Using default." << endl;
        }
    }
}


int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    if (argc != 2) 
    {
        cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
        return -1;
    }

    tutorial::AddressBook address_book;

    {
    // Read the existing address book.
    fstream input(argv[1], ios::in | ios::binary);
    if (!input) {
      cout << argv[1] << ": File not found.  Creating a new file." << endl;
    } else if (!address_book.ParseFromIstream(&input)) {
      cerr << "Failed to parse address book." << endl;
      return -1;
    }
    }

    // Add an address.
    PromptForAddress(address_book.add_person());

    {
    // Write the new address book back to disk.
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!address_book.SerializeToOstream(&output)) {
      cerr << "Failed to write address book." << endl;
      return -1;
    }
    }

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

接着在命令行执行以下命令编译:

g++ AddressBook.pb.cc main.cpp -o main `pkg-config --cflags --libs protobuf`
ls

编译成功的话会多出一个名为main的文件。
在这里插入图片描述执行main,会让你录入信息:

./main name.txt

在这里插入图片描述以上就是teminal下使用protobuf的所有过程,结束后你的工作空间应该如下:
在这里插入图片描述

三、cmake下使用protobuf

3.1 安装cmake

sudo apt-get install cmake cmake-qt-qui

3.2 编写CMakeLists文件

mkdir -p CSDN_ws/protobuf/cmake/
cp CSDN_ws/protobuf/teminal/AddressBook.proto CSDN_ws/protobuf/cmake/AddressBook.proto
cp CSDN_ws/protobuf/teminal/main.cpp CSDN_ws/protobuf/cmake/main.cpp
cd CSDN_ws/protobuf/cmake/
gedit CMakeLists.txt

将以下内容复制到CMakeLists.txt文件中:

cmake_minimum_required(VERSION 3.10)
PROJECT (cppTest)
SET(SRC_LIST main.cpp)

# Find required protobuf package
find_package(Protobuf REQUIRED)
if(PROTOBUF_FOUND)
    message(STATUS "protobuf library found")
else()
    message(FATAL_ERROR "protobuf library is needed but cant be found")
endif()

include_directories(${PROTOBUF_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS AddressBook.proto)

ADD_EXECUTABLE(cppTest ${SRC_LIST} ${PROTO_SRCS} ${PROTO_HDRS})

target_link_libraries(cppTest ${PROTOBUF_LIBRARIES})

3.3 cmake编译与运行

cd CSDN_ws/protobuf/cmake/
cmake .
make 
./cppTest  name.txt

运行结果和终端运行一样,最终文件目录如下所示:
在这里插入图片描述

四、VS code下使用protobuf

4.1 安装VS code

ubuntu 18.04进入ubuntu software输入visual studio code,安装:

在这里插入图片描述

去VS Code自带的商店下载的插件,快捷键:Ctrl+Shift+x,下载各种依赖包,包括:c/c++,c/c++ clang command adapter,c++ intellisense,CMake和CMake Tools如下图所示:
在这里插入图片描述

4.2

构建vscode文件夹,并打开vs code:

mkdir CSDN_ws/protobuf/vscode/
cp CSDN_ws/protobuf/cmake/AddressBook.proto CSDN_ws/protobuf/vscode/AddressBook.proto
cp CSDN_ws/protobuf/cmake/main.cpp CSDN_ws/protobuf/vscode/main.cpp
cp CSDN_ws/protobuf/cmake/CMakeLists.txt CSDN_ws/protobuf/vscode/CMakeLists.txt
cd CSDN_ws/protobuf/vscode/
code .

点击左侧的Debug按钮,选择添加配置(Add configuration),然后选择C++(GDB/LLDB),将自动生成launch.json文件如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/cppTest",
            "args": ["name.txt"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

在vs code界面点击F5,会提示要求配置task.json文件,将以下文本复制到默认task.json文件中:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "protobuf build",
			"command": "cmake . && make",
			"group": "build"
		}
	]
}

反回到main.cpp界面,点击F5,会显示编译消息,最终是运行结果:
在这里插入图片描述

最终文件目录如下所示:
在这里插入图片描述
如果你留恋这种顺畅,估计会发现以下这个不和谐的波浪线:

在这里插入图片描述
点击图中那个黄色的小灯泡,会出现几个小提示(这个小灯泡不影响编译,只是头文件不被VS code识别,无法自动补全),点击Add to "includePath":${workspaceFolder},会发现生多了一个c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

最后,VS code文件目录如下:
在这里插入图片描述


本文链接: http://www.dtmao.cc/news_show_100436.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?