libprotobuf ERROR google/protobuf/wire_format.cc:1059

本文介绍了解决使用google::protobuf进行序列化时遇到的UTF-8数据警告问题的方法。通过提供一个示例代码片段展示了如何将GBK编码转换为UTF-8编码,以避免序列化错误。

https://i-blog.csdnimg.cn/blog_migrate/d4e1423a47fc9a08404344926d7fbc3d.png

使用google::protobuf进行序列化,在运行时有如下警告!

libprotobuf ERROR google/protobuf/wire_format.cc:1059] Encountered string containing invalid UTF-8 data while serializing protocol buffer. Strings must contain only UTF-8; use the 'bytes' type for raw bytes.

libprotobuf ERROR google/protobuf/wire_format.cc:1059] Encountered string containing invalid UTF-8 data while serializing protocol buffer. Strings must contain only UTF-8; use the 'bytes' type for raw bytes.

原因:要求所有的string类型都必须为UTF-8类型的,可以使用<iconv.h> 进行格式转化。

介绍一下字符集相关知识:


在技术编码方面上,演化顺序为:ASCII ⇒ GB2312 ⇒ GBK ⇒ GB18030


先面贴一段转化的代码:

[cpp]  view plain copy
  1. include <string>  
  2. #include <stdlib.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5. #include <iconv.h>  
  6. bool convertGbk2Utf(string& instr, string& outstr)  
  7. {  
  8.                 iconv_t     gbk2UtfDescriptor;  
  9.                 gbk2UtfDescriptor = iconv_open("UTF-8""GBK");  
  10.                 size_t inlen = instr.length();  
  11.                 char* in = const_cast<char*>(instr.c_str());  
  12.                 size_t outlen = inlen * 2 + 1; // inlen * 1.5 + 1 >= outlen >= inlen + 1  
  13.                 char* outbuf = (char*)::malloc(outlen);  
  14.                 char* out = outbuf;  
  15.                 memset(outbuf, 0x0, outlen);  
  16.                 if((size_t)-1 == iconv(gbk2UtfDescriptor, &in, &inlen, &out, &outlen))  
  17.                 {  
  18.                                 ::free(outbuf);  
  19.                                 return false;  
  20.                 }  
  21.                 outstr.clear();  
  22.                 outstr.append(outbuf);  
  23.                 ::free(outbuf);  
  24.                 return true;  
  25. }  
  26. int main()  
  27. {  
  28.                 string str = "黄";  
  29.                 convertGbk2Utf(str,str);  
  30.                 cout << str << endl;  
  31. }  
<think> 我们正在解决一个关于编译错误的问题,用户遇到了找不到 'google/protobuf/runtime_version.h' 文件的问题。根据引用[1]的内容,虽然它提到了另一个错误(关于libhdf5_hl.so.100),但我们可以忽略它,因为用户当前的问题是不同的。 用户的问题:在编译过程中出现错误,提示 'google/protobuf/runtime_version.h: No such file or directory'。 这个错误通常发生在使用Protocol Buffers(protobuf)库时,系统找不到对应的头文件。可能的原因包括: 1. protobuf库没有正确安装。 2. 安装了多个版本的protobuf,而编译器在查找头文件时没有找到正确的版本。 3. 环境变量(如CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH)没有设置正确,导致编译器找不到头文件。 解决方案: 1. 确认protobuf是否已安装。我们可以通过在终端运行以下命令来检查: ```bash protoc --version ``` 如果未安装,则需要安装protobuf。 2. 如果已安装,可能是头文件不在编译器默认的搜索路径中。我们可以尝试定位头文件的位置: ```bash sudo find / -name 'runtime_version.h' ``` 找到后,将包含该头文件的目录添加到编译器的搜索路径中。例如,如果路径是`/usr/local/include`,则可以在编译命令中添加`-I/usr/local/include`。 3. 如果用户是通过源码编译protobuf,请确保执行了`make install`,并且安装路径在系统的包含路径中。 4. 如果用户使用包管理器安装,可以尝试重新安装protobuf的开发包。例如在Ubuntu上: ```bash sudo apt-get install libprotobuf-dev protobuf-compiler ``` 5. 如果系统中存在多个protobuf版本,可能需要指定版本或卸载冲突的版本。 6. 在编译项目时,确保使用的protobuf版本一致。如果是使用CMake,可以在CMakeLists.txt中指定protobuf的路径。 7. 设置环境变量(如果必要): ```bash export CPATH=/path/to/protobuf/include:$CPATH ``` 由于用户之前提到过引用[1](虽然不直接相关),但我们可以参考其上下文,用户可能在编译caffe项目,因此我们也可以考虑检查caffe的编译配置中是否指定了正确的protobuf路径。 注意:在编译caffe时,可能会使用到protobuf,因此确保caffe的Makefile.config中包含了正确的protobuf路径。 综上所述,我们给出以下步骤: 步骤1:检查protobuf是否安装 步骤2:定位头文件并添加到编译器搜索路径 步骤3:重新安装protobuf(如果需要) 步骤4:检查并设置环境变量 步骤5:如果使用caffe,检查Makefile.config配置 下面我们详细说明: ### 解决 'google/protobuf/runtime_version.h' 找不到的问题 1. **检查protobuf安装情况** 在终端输入: ```bash protoc --version ``` 如果显示版本信息(例如`libprotoc 3.19.4`),则protobuf已安装。如果未安装,请根据操作系统安装。 Ubuntu/Debian: ```bash sudo apt-get update sudo apt-get install libprotobuf-dev protobuf-compiler ``` CentOS/RHEL: ```bash sudo yum install protobuf-devel protobuf-compiler ``` 2. **查找头文件位置** 如果已安装但找不到头文件,使用以下命令查找: ```bash sudo find /usr -name 'runtime_version.h' 2>/dev/null ``` 或者 ```bash sudo find / -name 'runtime_version.h' 2>/dev/null ``` 如果找到,记下其所在目录(例如`/usr/local/include/google/protobuf/runtime_version.h`,那么包含目录为`/usr/local/include`)。 3. **添加包含路径** 在编译命令中加入包含路径,例如: ```bash g++ ... -I/usr/local/include ... ``` 如果使用CMake,可以在CMakeLists.txt中添加: ```cmake include_directories(/usr/local/include) ``` 4. **重新安装protobuf** 如果未找到头文件,可能需要重新安装protobuf。建议从源码安装最新版本(或所需版本): ```bash # 下载源码(以3.19.4为例) wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protobuf-all-3.19.4.tar.gz tar -xzf protobuf-all-3.19.4.tar.gz cd protobuf-3.19.4 ./configure --prefix=/usr/local make make check sudo make install # 更新共享库缓存 sudo ldconfig ``` 5. **设置环境变量** 如果头文件安装在非标准路径,需要设置环境变量: ```bash export CPATH=/path/to/include:$CPATH ``` 将其添加到`~/.bashrc`或`~/.zshrc`中永久生效。 6. **检查caffe的Makefile.config(如果适用)** 如果您在编译caffe,请打开`Makefile.config`,查看是否有设置`INCLUDE_DIRS`,确保其中包含了protobuf头文件所在目录。 7. **确保版本一致性** 如果您的项目要求特定版本的protobuf,请确保安装的版本符合要求。 如果上述步骤仍不能解决问题,请提供更多关于您的环境和操作的信息,以便进一步排查。 §§相关问题§§ 1. 如何安装特定版本的protobuf? 2. 在CMake中如何正确配置protobuf? 3. 编译caffe时遇到protobuf相关的错误还有哪些常见原因? </think> ### 解决 `google/protobuf/runtime_version.h: No such file or directory` 错误的方法 该错误通常发生在编译依赖 Protocol Buffers (protobuf) 的项目时,系统找不到 protobuf 的开发头文件。以下是系统性的解决方案: #### 1. **安装 Protocol Buffers 开发包** 确保已安装 protobuf 的开发库和头文件: ```bash # Ubuntu/Debian sudo apt-get install libprotobuf-dev protobuf-compiler # CentOS/RHEL sudo yum install protobuf-devel protobuf-compiler # macOS (Homebrew) brew install protobuf ``` 验证安装: ```bash protoc --version # 应显示版本号(如 libprotoc 3.19.4) ``` #### 2. **检查头文件路径** 确认头文件是否存在于标准路径: ```bash # 查找头文件位置 sudo find / -name 'runtime_version.h' 2>/dev/null ``` - 若路径为 `/usr/local/include/google/protobuf/...`,需确保编译器包含该路径(见步骤 3)。 - **若未找到**:重新安装 protobuf 或从源码编译(见步骤 4)。 #### 3. **配置编译器包含路径** 在编译命令中添加 protobuf 头文件路径: ```bash # 示例:手动指定路径 g++ your_code.cpp -I/usr/local/include -o output ``` - **CMake 项目**:在 `CMakeLists.txt` 中添加: ```cmake include_directories(/usr/local/include) # 替换为实际路径 find_package(Protobuf REQUIRED) target_include_directories(your_target PRIVATE ${Protobuf_INCLUDE_DIRS}) ``` #### 4. **从源码编译安装 protobuf** 若包管理器安装无效,手动编译: ```bash # 下载源码(替换为最新版本号) wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protobuf-all-3.19.4.tar.gz tar -xzf protobuf-all-3.19.4.tar.gz cd protobuf-3.19.4 # 编译安装 ./configure --prefix=/usr/local make sudo make install # 更新动态库缓存 sudo ldconfig ``` #### 5. **检查多版本冲突** 若系统存在多个 protobuf 版本: ```bash # 查看所有已安装版本 locate libprotobuf.so ``` - **解决方案**: 1. 卸载冲突版本:`sudo apt remove libprotobuf-dev` 2. 在编译命令中显式指定路径:`-I/path/to/correct/protobuf/include` #### 6. **验证环境变量** 确保 `PKG_CONFIG_PATH` 包含 protobuf 的 `.pc` 文件路径: ```bash export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH pkg-config --cflags protobuf # 应输出包含路径 ``` #### 7. **项目特定配置(如 Caffe)** 若错误出现在编译 Caffe 等框架: - 检查 `Makefile.config` 中的 `INCLUDE_DIRS`: ```makefile INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include ``` - 确保未注释 `PROTOBUF_DIR`(若有)。 > **原理说明**: > 此错误表明编译器在预定义路径(如 `/usr/include`)中未找到 protobuf 的开发头文件,通常由安装不完整、路径未配置或多版本冲突导致。通过显式指定头文件目录或重新安装开发包可解决[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值