安装protobuf-cpp.3.5.1报错,报错内容如下:
./google/protobuf/stubs/hash.h: In instantiation of 'google::protobuf::hash<long long int>':
./google/protobuf/map_field.h:817: instantiated from here
./google/protobuf/stubs/hash.h:336: error: invalid use of undefined type 'struct std::tr1::hash<long long int>'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/functional:1101: error: declaration of 'struct std::tr1::hash<long long int>'
./google/protobuf/map_field.h: In member function 'size_t google::protobuf::hash<google::protobuf::MapKey>::operator()(const google::protobuf::MapKey&) const':
./google/protobuf/map_field.h:817: error: no match for call to '(google::protobuf::hash<long long int>) (google::protobuf::int64)'
./google/protobuf/stubs/hash.h: At global scope:
./google/protobuf/stubs/hash.h: In instantiation of 'google::protobuf::hash<long long unsigned int>':
./google/protobuf/map_field.h:821: instantiated from here
./google/protobuf/stubs/hash.h:336: error: invalid use of undefined type 'struct std::tr1::hash<long long unsigned int>'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/functional:1101: error: declaration of 'struct std::tr1::hash<long long unsigned int>'
./google/protobuf/map_field.h: In member function 'size_t google::protobuf::hash<google::protobuf::MapKey>::operator()(const google::protobuf::MapKey&) const':
./google/protobuf/map_field.h:821: error: no match for call to '(google::protobuf::hash<long long unsigned int>) (google::protobuf::uint64)'
make[2]: * [google/protobuf/dynamic_message.lo] Error 1
make[2]: Leaving directory `/root/protobuf-3.5.1/src'
make[1]: * [all-recursive] Error 1
make[1]: Leaving directory `/root/protobuf-3.5.1'
make: * [all] Error 2
安装步骤:
$ tar -zxvf protobuf-cpp-3.5.1.tar.gz
$ ./configure
$ make
protobuf-cpp是一个在C++编译器上支持protobuf(Protocol Buffers)的库。以下是一些可能导致protobuf-cpp在Makefile中报错的原因和解决方法:
protobuf-cpp := protobuf-cpp
或者将protobuf-cpp包含到你的项目的include
或dependency
文件夹中:
include protobuf-cpp
如果protobuf库已包含在你的项目中,但编译时还是报错,可能是因为protobuf库的版本与你的C++编译器或protobuf-cpp的版本不兼容。在Makefile中,你可以使用proto_cxx_library
或protocxx_library
来指定protobuf库的版本:
proto_cxx_library := $(protobuf-cpp)
proto_cxx_library := $(proto_cxx_library)/$(PROTOBUF_VERSION)
proto_cxx_library := $(proto_cxx_library)/$(PROTOBUF_VERSION)-$(CXX_COMPILER_VERSION)
例如,如果你的protobuf库为protocxx
,且你使用的是C++17编译器,你可以在你的Makefile中使用以下版本的proto_cxx_library
:
proto_cxx_library := $(protobuf-cpp)/$(PROTOBUF_VERSION)-$(CXX_COMPILER_VERSION)
protobuf-cpp编译错误: protobuf-cpp可能在编译过程中出现错误,这可能导致编译器报错。以下是一些可能的错误类型和解决方案:
拼写错误:在编译protobuf-cpp时,确保所有protobuf头文件(例如protoc.h
)的拼写都是正确的。例如,<protobuf.h>
可能被误拼为<protoc.h.proto>
。
缺少.proto文件:在编译protobuf-cpp时,确保所有proto文件(例如my.proto
)都被正确地编译。检查每个proto文件是否在include
或dependency
文件夹中,并确保它们被正确地链接到protobuf库中。
错误的protobuf头文件:确保你的protobuf头文件中没有包含必要的注释、结构体定义或其他定义。例如,如果你的protobuf头文件中包含struct MyStruct
,但没有在protobuf文件中定义,编译器可能会抛出错误。
错误的protobuf结构体定义:在protobuf结构体定义中,确保所有的字段名、属性名、方法名等都是有意义的,并且遵循protobuf的命名规则。例如,如果你的protobuf结构体定义中使用了field1
,但未定义该字段,编译器可能会抛出错误。
缺少proto编译选项:在编译protobuf-cpp时,可能需要使用特定的protobuf编译选项。例如,如果你在编译时使用了--cpp_out
选项,但没有指定protobuf库的输出目录,编译器可能会报错。
错误的protobuf编译器选项:在编译protobuf-cpp时,可能需要使用特定的protobuf编译器选项。例如,如果你在编译时使用了--cpp_incompatible
选项,但没有指定protobuf库的不兼容选项,编译器可能会报错。
错误的protobuf库路径:确保protobuf库的路径在Makefile中正确地指定。例如,如果你在你的Makefile中指定了protobuf-cpp
的路径,但在编译时指定的路径与protobuf库的实际路径不一致,编译器可能会报错。
缺少protobuf库的依赖项:在编译protobuf-cpp时,可能需要使用特定的protobuf库依赖项。例如,如果你的protobuf库没有包含在你的项目中,但编译protobuf-cpp时需要使用特定的protobuf库依赖项,例如google/protobuf
,则需要在你的Makefile中指定这些依赖项:
makefile
proto_cxx_library := $(protobuf-cpp)/$(PROTOBUF_VERSION)-$(CXX_COMPILER_VERSION)-$(CXX_COMPILER的那个版本)
proto_cxx_library := $(proto_cxx_library)/$(PROTOBUF_VERSION)-$(CXX_COMPILER的那个版本)-$(CXX_COMPILER的那个版本)
proto_cxx_library := $(proto_cxx_library)/$(PROTOBUF_VERSION)-$(CXX_COMPILER的那个版本)-$(CXX_COMPILER的那个版本)-$(CXX_COMPILER的那个版本)-$(CXX_COMPILER的那个版本)
注意:在使用protobuf-cpp依赖项时,你需要将CXX_COMPILER的那个版本
替换为你的实际C++编译器的版本。
以上是可能导致protobuf-cpp在Makefile中报错的一些常见原因和解决方案,如果问题仍然存在,可能需要对protobuf-cpp的源代码或编译器进行更深入的检查和调试。