大家好,我是农场主,一个不会造轮子只会复制粘贴的代码搬运工,在这里会记录一些编程,记录一下踩过的坑,让生命留下一丝痕迹,如果看完了觉得对您有些帮助的话,欢迎关注,感谢分享!
今天在做一个 Grpc 的小测试中,引入了 "google/protobuf/timestamp.proto" 中的 timestamp 使用类型,在生成代码的时候报下面的错误:
google/protobuf/timestamp.proto: File not found.
message/message.proto:5:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
message/message.proto:21:5: "google.protobuf.Timestamp" is not defined.
搞不明白啊,为什么会这样,然后就 google,千奇百怪的回答。没有找到答案,可能是我找答案的姿势不对吧。
后面想想,使用最棒的重装大法吧。
在 官方包 下载了一个最新的 protoc。
下载解压后,发现里面有两个目录和一个文件,一个是 bin 和 readme.txt 还有一个是 include,之前安装的时候没怎么管这个 include。下意识的点开 readme.txt,惊喜。
Protocol Buffers - Google's data interchange format
Copyright 2008 Google Inc.
https://developers.google.com/protocol-buffers/
This package contains a precompiled binary version of the protocol buffer
compiler (protoc). This binary is intended for users who want to use Protocol
Buffers in languages other than C++ but do not want to compile protoc
themselves. To install, simply place this binary somewhere in your PATH.
If you intend to use the included well known types then don't forget to
copy the contents of the 'include' directory somewhere as well, for example
into '/usr/local/include/'.
Please refer to our official github site for more installation instructions:
https://github.com/protocolbuffers/protobuf
里面有句话 If you intend to use the included well known types then don't forget to copy the contents of the 'include' directory somewhere as well, for example into '/usr/local/include/'.
尝试着把 include/google 移动到 /usr/local/include/google 然后重新生成。没有报错,正确生成代码。
做完之后思考了下。
可能在我们使用 protoc 生成目标代码的时候,这些类型不是基础类型, google 在设计的时候就没有把这些编译到 protoc 里面,而是根据他在文档里面指定的 /usr/local/include/ 文件夹去寻找这些类型 .proto 文件。
回顾这个问题的原因,感觉还是自己不仔细导致的问题,有点浪费时间。好在问题是成功解决了,