ubuntu20.04下从源码编译安装protobuf-3.20.2.0并在c++项目中使用

一.版本选择

选择prorobuf3-20.2.0
https://github.com/protocolbuffers/protobuf/releases/tag/v3.20.2
选择的是protobuf-cpp-3.20.2.tar.gz

二.编译安装

cd protobuf-3.20.2/
mkdir build-make
cd build-make
sudo cmake ./../cmake/
# 等待cmake完成......
sudo make install
# 看到install提示的include目录和lib目录以及bin目录

然后手动将include文件夹和lib文件夹拷贝出来(看自己习惯)
根据自己习惯准备生成好的include,lib以及bin下的文件以及库

三.使用流程

1.定义协议文件

people.proto

syntax = "proto3";
package school;

message people {
  string name = 1;
  int32 age = 2;
  int32 scores = 3;
}

2.生成协议对应cpp文件

通过刚刚make install生成的可执行文件protoc生成代码

protoc \
# proto协议文件位置
--proto_path=/home/shecannotsee/sheTestcode/prorobuf_test/ \
# 生成的cpp代码文件存放的位置
--cpp_out=/home/shecannotsee/sheTestcode/prorobuf_test/protobuf/ \
# 指定协议进行代码生成
people.proto

3.编译运行

目录结构

shecannotsee@ubuntu:~/sheTestcode/prorobuf_test$ tree
.
|-- CMakeLists.txt
|-- README.md
|-- header.h
|-- main.cpp
|-- people.proto
`-- protobuf
    |-- people.pb.cc
    `-- people.pb.h

1 directory, 7 files

header.h

#ifndef PROTOBUF_TEST_PROROBUF_TEST_HEADER_H_
#define PROTOBUF_TEST_PROROBUF_TEST_HEADER_H_
#include 
#include 
#include "protobuf/people.pb.h"

namespace method1 {
void main() {
  // Serialization
  school::people Jerry;
  Jerry.set_name("Jerry");
  Jerry.set_age(15);
  Jerry.set_scores(686);
  std::string buff;
  Jerry.SerializeToString(&buff);

  // parse
  school::people Jerry_father;
  if (!Jerry_father.ParseFromString(buff))
    std::cout<<"parse error.\n";

  std::cout<<"Jerry_father's want to know Jerry's name   is :"<<Jerry_father.name()   <<std::endl;
  std::cout<<"Jerry_father's want to know Jerry's age    is :"<<Jerry_father.age()    <<std::endl;
  std::cout<<"Jerry_father's want to know Jerry's scores is :"<<Jerry_father.scores() <<std::endl;
};
};// namespace method1
#endif //PROTOBUF_TEST_PROROBUF_TEST_HEADER_H_

main.cpp

#include 
#include "header.h"
int main() {
  std::cout<<"protobuf test start.\n";
  method1::main();
  std::cout<<"end.\n";
};

CMakeLists.txt
路径:
– sheTestcode’s path:/home/shecannotsee/sheTestcode
– father_dir’s path:/home/shecannotsee
– protobuf_dir /home/shecannotsee/libraries/protobuf-3.20.2

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJECT_NAME "protobuf_test")
set(CMAKE_CXX_FLAGS "-g")# 断点无效处理方案
project(${PROJECT_NAME})

get_filename_component(sheTestcode_dir ${CMAKE_SOURCE_DIR} DIRECTORY)
get_filename_component(father_dir ${sheTestcode_dir} DIRECTORY)
message(STATUS "sheTestcode's path:${sheTestcode_dir}")
message(STATUS "father_dir's path:${father_dir}")
set(protobuf_dir "${father_dir}/libraries/protobuf-3.20.2")
message(STATUS "protobuf_dir ${protobuf_dir}")

# include
include_directories(
		# 也许要加入生成的头文件目录?
        ${protobuf_dir}/include #add protobuf
)
# lib
link_directories(
        ${protobuf_dir}/lib #add protobuf
)

# aux_source_directory(. SOURCE)
file(GLOB_RECURSE SRC "${CMAKE_SOURCE_DIR}/main.cpp")
# 加入生成的cpp文件进行编译
file(GLOB_RECURSE protobuf_code "${CMAKE_SOURCE_DIR}/protobuf/*.cc")  #add protobuf
add_executable(${PROJECT_NAME}
        ${SRC}
        ${protobuf_code}# add protobuf
        )

# 链接库
target_link_libraries(${PROJECT_NAME} "-pthread" protobuf)# add protobuf

结果如下

/home/shecannotsee/sheTestcode/prorobuf_test/cmake-build-debug/protobuf_test
protobuf test start.
Jerry_father's want to know Jerry's name   is :Jerry
Jerry_father's want to know Jerry's age    is :15
Jerry_father's want to know Jerry's scores is :686
end.

进程已结束,退出代码为 0

可以在该地址下protobuf_test找到完整代码
https://github.com/shecannotsee/sheTestcode

你可能感兴趣的:(ubuntu,c++,linux)