protoc初识

news2024/9/23 7:25:30

protoc初识

参考资料

https://blog.csdn.net/qq_46637011/article/details/135085568

https://blog.csdn.net/qq_39400324/article/details/134172333

一 前期准备

windows查看proto是否安装好及版本号

protoc --version

windows查看路径下文件

dir

想在124.50机器上安装protobuf,执行build.sh脚本的时候报错:

configure.ac:109: error: possibly undefined macro: AC_PROG_LIBTOOL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.

使用如下安装命令报错

apt-get install libtool
-bash: apt-get: command not found

百度查到这个指令,用于centOS系统

sudo yum install libtool

安装成功

二、实现功能

2.1 124.43实现效果

在124.43上实现了proto的反序列化

但是过程很曲折,甚至反序列化成功的时候我还是懵的,而且感觉其中还是有问题,但是聊胜于无,

root@0002:~/zhuyfTest/protobuf/myFirstTest/bu# ./myFirstTest
10, man, lucy, 32
ʹ▒▒<windows.h>ͷ▒ļ▒▒▒▒▒▒ļ▒
ʹ▒▒<dirent.h>ͷ▒ļ▒▒▒▒▒▒ļ▒
Hello, World!

2.2 解决两个报错

下面描述一下整个过程

首先我在Clion上调试失败了,都找不到远程服务器了。直接本地调试报错

D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: cannot find -lprotobuf: No such file or directory
D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: cannot find -lprotoc: No such file or directory
collect2.exe: error: ld returned 1 exit status

我就直接在124.50机器上直接cmake,但是报错

/usr/bin/ld: CMakeFiles/main.cpp.dir/main.cpp.o: in function test()': /data2/myFirstTest/main.cpp:172: undefined reference to google::protobuf::MessageLite::SerializeToString(std::__cxx11::basic_string<char, std::char_traits, std::allocator >*) const’
/usr/bin/ld: /data2/myFirstTest/main.cpp:176: undefined reference to google::protobuf::MessageLite::ParseFromString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /usr/bin/ld: /data2/myFirstTest/main.cpp:175: undefined reference to Person::~Person()’
/usr/bin/ld: /data2/myFirstTest/main.cpp:164: undefined reference to Person::~Person()' /usr/bin/ld: /data2/myFirstTest/main.cpp:175: undefined reference to Person::~Person()’
/usr/bin/ld: /data2/myFirstTest/main.cpp:164: undefined reference to `Person::~Person()’

我已查看protoc --version,查不到版本号,我试着用protobuf的编译脚本编译protobuf,编译好了查看版本号还是没有版本号,还是不行。

#!/bin/bash

set -ex

PREFIX_INC_PATH=$(cd "$(dirname "$0")";pwd)

mv protobuf-21.9 protobuf
chmod -R 777 protobuf
cd protobuf
./autogen.sh
./configure --prefix=$PREFIX_INC_PATH/install --with-pic

make -j 32
make install

export LD_LIBRARY_PATH=${PREFIX_INC_PATH}/install/lib:$LD_LIBRARY_PATH

我就跑到124.43上试试,因为124.43上可以查到protoc版本号

protoc --version
libprotoc 3.21.9

我执行cmake编译,有报错,而且报错也和124.50差不多,找不到定义

undefined reference to google::protobuf::MessageLite::SerializeToString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) const' /usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:176: undefined reference to google::protobuf::MessageLite::ParseFromString(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)’
/usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:175: undefined reference to Person::~Person()' /usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:164: undefined reference to Person::~Person()’
/usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:175: undefined reference to Person::~Person()' /usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:164: undefined reference to Person::~Person()’

但是编译出可执行文件了,我一执行,成功打印出信息。

现在起码是看到效果了,再继续看看。

目前的代码情况是虽然打印出结果了,但是还是有报错信息,报错信息如下

/usr/local/bin/ld: DWARF error: could not find variable specification at offset 2094
/usr/local/bin/ld: DWARF error: could not find variable specification at offset 20f0
/usr/local/bin/ld: DWARF error: could not find variable specification at offset 2158
/usr/local/bin/ld: DWARF error: could not find variable specification at offset e83f
/usr/local/bin/ld: DWARF error: could not find variable specification at offset e84b
...
...
CMakeFiles/main.cpp.dir/main.cpp.o: in function `test()':
/root/zhuyfTest/protobuf/myFirstTest/main.cpp:172: undefined reference to `google::protobuf::MessageLite::SerializeToString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) const'
/usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:176: undefined reference to `google::protobuf::MessageLite::ParseFromString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:175: undefined reference to `Person::~Person()'
/usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:164: undefined reference to `Person::~Person()'
/usr/local/bin/ld: /root/zhuyfTest/protobuf/myFirstTest/main.cpp:175: undefined reference to `Person::~Person()'

针对第一个问题,/usr/local/bin/ld: DWARF error: could not find variable specification at offset 2094

解决办法是将CMakeList.txt语句

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -D_DEBUG")
//改成
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -D_DEBUG")

原因不知道,参考:https://blog.csdn.net/chen134225/article/details/126316536

第二个问题的原因是我的CmakeList问题,

有误版CmakeList

cmake_minimum_required(VERSION 3.16.5)
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} CXX)

#set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -D_DEBUG")

if (PLATFORM STREQUAL WINDOWS OR PLATFORM STREQUAL windows)
    SET(PLATFORM windows)
    add_definitions(
            -DWIN32
    )
else ()
    SET(PLATFORM linux)
    add_definitions(
            -Dlinux
    )
endif()

include_directories(../myFirstTest/3rd/protobuf/auto/include)
include_directories(../myFirstTest/3rd/protobuf/src)
include_directories(../myFirstTest/include)

aux_source_directory(../myFirstTest/3rd/protobuf/auto/src protobufSrc)

link_directories(../myFirstTest/3rd/protobuf/lib/linux)

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
foreach(file ${files})
    get_filename_component(name ${file} NAME)
    add_executable(${name} ${file}
            include/protobufProtocal.h)
endforeach()

add_executable(myFirstTest
        ${protobufSrc}
        main.cpp)

TARGET_LINK_LIBRARIES(myFirstTest protobuf protoc)

成功版本

cmake_minimum_required(VERSION 3.16.5)
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} CXX)

#set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -D_DEBUG")

if (PLATFORM STREQUAL WINDOWS OR PLATFORM STREQUAL windows)
    SET(PLATFORM windows)
    add_definitions(
            -DWIN32
    )
else ()
    SET(PLATFORM linux)
    add_definitions(
            -Dlinux
    )
endif()

include_directories(../myFirstTest/3rd/protobuf/auto/include)
#include_directories(../myFirstTest/3rd/protobuf/src/google/protobuf)
include_directories(../myFirstTest/3rd/protobuf/src)
include_directories(../myFirstTest/include)

aux_source_directory(../myFirstTest/3rd/protobuf/auto/src protobufSrc)

#link_directories(../myFirstTest/3rd/protobuf/lib/linux)

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
foreach(file ${files})
    get_filename_component(name ${file} NAME)
    add_executable(${name} ${file})
endforeach()

add_executable(myFirstTest
        ${protobufSrc}
        main.cpp)

TARGET_LINK_LIBRARIES(myFirstTest protobuf protoc)

问题出现在这里

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
foreach(file ${files})
    get_filename_component(name ${file} NAME)
    add_executable(${name} ${file})
endforeach()

注释掉这些语句,然后重新cmake,再make,就能编译成功了,执行./myFirstTest,执行成功

root@0002:~/zhuyfTest/protobuf/myFirstTest/buildTest# ./myFirstTest
10, man, lucy, 32
Hello, World!

2.4 windows环境Clion远程调试实现效果

以上就是在linux环境下将protobuf序列化和反序列化实现。然后就回到一开始出现的问题,windows环境Clion没有执行成功。

报错

D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: CMakeFiles/myFirstTest.dir/3rd/protobuf/auto/src/firstVersion.pb.cc.obj: in function Person::Person(Person const&)': D:/AppData/CLion/myFirstTest/3rd/protobuf/auto/src/firstVersion.pb.cc:112: undefined reference to google::protobuf::internal::ArenaStringPtr::Set(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, google::protobuf::Arena*)’
D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: D:/AppData/CLion/myFirstTest/3rd/protobuf/auto/src/firstVersion.pb.cc:120: undefined reference to google::protobuf::internal::ArenaStringPtr::Set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::Arena*)' D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: CMakeFiles/myFirstTest.dir/3rd/protobuf/auto/src/firstVersion.pb.cc.obj: in function Person::Clear()‘:
D:/AppData/CLion/myFirstTest/3rd/protobuf/auto/src/firstVersion.pb.cc:175: undefined reference to google::protobuf::internal::ArenaStringPtr::ClearToEmpty()' D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: D:/AppData/CLion/myFirstTest/3rd/protobuf/auto/src/firstVersion.pb.cc:176: undefined reference to google::protobuf::internal::ArenaStringPtr::ClearToEmpty()’
D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: CMakeFiles/myFirstTest.dir/3rd/protobuf/auto/src/firstVersion.pb.cc.obj: in function `Person::_InternalParse(char const*, google::protobuf::internal::ParseContext*)':

这时候如果把下面的语句注释掉,

link_directories(../myFirstTest/3rd/protobuf/lib/linux)

则不出现上述报错,出现下述报错

显示信息

D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: cannot find -lprotobuf: No such file or directory
D:\APP\CLion\CLion 2023.2.2\bin\mingw\bin/ld.exe: cannot find -lprotoc: No such file or directory

我认为这边是因为linux环境下无法使用.a文件,需要配置lib静态库文件导致的。

然后我又想到再试下远程调试,还是不能远程调试。我把setting里面的Cmake反复切换也是不行,然后突然想到是不是应该把工程中的几个名称如[cmake-build-debug-*]等文件夹删掉?

删除文件夹试了一下,可以远程调试,执行成功,成功打印出反序列化数据。

2.5 windows环境CLion本地实现效果

2.5.1 安装cmake

cmake安装参考教程:https://blog.csdn.net/jlm7689235/article/details/131087421

windows编译protobuf静态库文件参考教程:https://blog.csdn.net/g9208/article/details/102640357

下载cmake:https://cmake.org/download/网址
在这里插入图片描述

windows安装zlib:https://blog.csdn.net/Strive_For_Future/article/details/134080450

2.5.2 cmake编译静态库文件

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1616558.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux进程概念(四):进程优先级 环境变量

目录 进程优先级 学前补充 命令行参数 环境变量 环境变量$PATH 整体理解环境变量 environ指针 获取环境变量的三种方式 内建指令 证明内建指令的存在 进程优先级 基本概念&#xff1a;指定进程获取某种资源的先后顺序 linux的优先级是进程控制块task_struct中的…

华为交换机重置密码

1.进入bootrom 加电后&#xff0c;18S左右&#xff0c;在启动菜单按 CtrlB 进入bootrom&#xff08;3s内&#xff09; 注意&#xff1a;本步骤属于高危操作&#xff0c;一定小心切勿删除系统或修改bootrom密码&#xff01; 输入bootrom密码&#xff0c;按6 看到提示成功后按…

Django模型的继承

Django模型的继承 Django模型的继承&#xff0c;包括模型的抽象基类、Meta继承、related_name和related_query_name属性、多表继承、Meta和多表继承、继承与反向关系、代理模型、代理模型继承和未托管模型&#xff0c;以及多重继承等内容。本文讲解一下抽象基类继承&#xff0…

详解Qt中实现树状结构图

在Qt中&#xff0c;实现树状结构图通常采用QTreeWidget或QTreeView组件。这两个组件都允许我们创建具有层次结构的列表&#xff0c;但它们之间存在一些差异。QTreeWidget提供了更简单的API&#xff0c;适用于轻量级、快速开发的需求&#xff1b;而QTreeView则更为灵活和可定制&…

.NET WebAPI服务端启用跨源请求 (CORS)

跨源请求 (CORS)问题 在API服务端启用跨源请求 (CORS) 调用 UseCors 扩展方法并指定 PolicyCorsName CORS 策略。 UseCors 添加 CORS 中间件。 对 UseCors 的调用必须放在 UseRouting 之后&#xff0c;但在 UseAuthorization 之前。 Program.cs添加如下代码&#xff08;注意中间…

货拉拉0-1数据指标体系构建与应用

目录 一、背景 二、指标体系搭建 2.1 指标设计 2.2 指标体系搭建 2.3 指标维度拆解 三、指标标准化建设 四、指标元数据管理 五、指标应用&未来规划 原文大佬介绍的这篇指标体系构建有借鉴意义&#xff0c;现摘抄下来用作沉淀学习。如有侵权请告知~ 一、背景 指标…

二叉搜索树及相关操作(图文详解)

1.概念 二叉搜索树又称二叉排序树&#xff0c;它或者是一棵空树&#xff0c;或者是具有以下性质的二叉树: 若它的左子树不为空&#xff0c;则左子树上所有节点的值都小于根节点的值 若它的右子树不为空&#xff0c;则右子树上所有节点的值都大于根节点的值 它的左右子树也分…

【面试经典 150 | 数组】整数转罗马数字

文章目录 写在前面Tag题目来源解题思路方法一&#xff1a;模拟 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法&#xff0c;两到三天更新一篇文章&#xff0c;欢迎催更…… 专栏内容以分析题目为主&#xff0c;并附带一些对于本题涉及到的数据结构等内容进行回顾…

c# 构造函数 静态构造函数 内联字段(即静态字段和实例字段) 父类构造函数 父类静态构造函数 父类内联字段 执行顺序

顺序如下&#xff1a; 1.子类的内联字段 2.子类的静态构造函数 3.父类的内联字段 4.父类的静态构造函数 5.父类的构造函数 6.子类的构造函数 7.子类的方法 public class A{public static string a1"A0";static A(){Console.WriteLine("父类内联字段&#xff1a;…

品深茶的创始人是谁?

据说&#xff0c;品深茶的创始人之前是一个程序员&#xff0c;他在软件行业工作十多年&#xff0c;由于常年熬夜加班再加上抽烟喝酒等不良习惯&#xff0c;导致在一次体检中被查出患上了肾癌&#xff0c;对他来说&#xff0c;期待的财务自由还没实现&#xff0c;身体就已经完蛋…

C++进阶复习

1 指针 作用&#xff1a;通过指针间接访问内存 1.内存编号是从0开始记录的&#xff0c;一般用十六进制数字表示。 2.可以利用指针变量保存地址。 1.1 指针变量的定义和使用 语法&#xff1a;数据类型 *变量名。如int *a; #include<iostream> using namespace std;int mai…

mininet+odl安装

安装环境 ubuntu-18.04.2-desktop-amd64 Java version: 1.8.0_362 Apache Maven 3.6.0 opendaylight: distribution-karaf-0.6.0-Carbon(csdn中应该是已有资源&#xff0c;不让上传) opendaylight的官网下载链接一直打开失败&#xff0c;我使用的是别人的Carbon版本。 在安…

汇编语言作业(三)

目录 一、实验目的 二、实验内容 三、实验步骤以及结果 1、阅读第4页ppt 第一个汇编程序 ex1.asm 并编写编译执行成功后&#xff0c;要求改写&#xff0c;使其输出字符ABC&#xff0c;如下图所示。 &#xff08;1&#xff09;编译产生目标文件 &#xff08;2&#xff09;…

Python-VBA函数之旅-hex函数

目录 一、hex函数常见的应用场景&#xff1a; 二、hex函数使用注意事项&#xff1a; 1、hex函数&#xff1a; 1-1、Python&#xff1a; 1-2、VBA&#xff1a; 2、推荐阅读&#xff1a; 个人主页&#xff1a;神奇夜光杯-CSDN博客 一、hex函数常见的应用场景&#xff1a;…

【STM32+HAL+Proteus】系列学习教程---串口USART(DMA 方式)定长,不定长收发。

实现目标 1、利用UART实现上位机PC与下位机开发板之间的数据通信 2、学会STM32CubeMX软件关于UART的DMA模式配置 3、具体目标&#xff1a;1、实现串口定长收发数据通信&#xff1b;2、串口不定长收发数据通信。 一、DMA简介 1、什么是DMA? DMA&#xff08;Direct Memory …

基于YOLOV5 的ROS2功能包——快速实现目标识别功能

项目链接&#xff1a; yolov5_ros2 运行结果如下&#xff1a;实时显示识别结果 一、下载功能包并运行 1. 安装依赖 首先&#xff0c;确保您已经更新了系统并且安装了必要的依赖。以下是一些安装步骤&#xff0c;其中$ROS_DISTRO 是您的ROS2发行版&#xff08;例如&#xff1…

交通工程绪论

一、交通工程 交通工程学定义交通工程学研究的内容交通工程学的产生与发展交通工程学在道路运输管理中的作用 1. 交通工程学定义 早在20世纪30年代&#xff0c;美国交通工程师协会(American Institute of Traffic Engineers)给交通工程学(Traffic Engineering)下了一个定义&a…

去雾笔记-Pixel Shuffle,逆Pixel Shuffle,棋盘效应,转置卷积

文章目录 1.Pixel Shuffle2.Inverse Pixel Shuffle3.棋盘效应4.转置卷积5.宽激活块6.PSPNet7.反射填充层&#xff08;Reflective Padding Layer&#xff09;8.tanh层 1.Pixel Shuffle Pixel Shuffle是一种用于图像超分辨率的技术&#xff0c;它通过重新排列图像的像素来增加图…

Django模型的属性与方法

本节介绍Django模型的属性和方法&#xff0c;以及如何重写之前定义的模型方法等内容。 3.5.1 模型属性 Django模型中最重要的属性就是Manager&#xff0c;它是Django模型和数据库查询操作之间的接口&#xff0c;并且被用作从数据库当中获取实例的途径。如果Django模型中没有…

python基础知识一(注释、变量以及类型、类型转换)

目录 注释&#xff1a; 注释分为两种&#xff1a; 注释的作用&#xff1a; 注释的使用原则&#xff1a; 编写一段代码&#xff0c;对比一下有无注释的区别&#xff0c;以冒泡排序为例 1. 无注释版&#xff1a; 感官上是不是有点不清晰&#xff1f; 2. 有注释版&#xff1…