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 togoogle::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 toPerson::~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 toPerson::~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 toPerson::~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 togoogle::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 togoogle::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