遇到的问题:Could not import the Google protobuf Python libraries
解决办法[1]:
pip install --ignore-installed six
sudo pip install protobuf
cmake_minimum_required(VERSION 2.8)
project(NANOPB_CMAKE_SIMPLE C)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../extra)
find_package(Nanopb REQUIRED)
include_directories(${NANOPB_INCLUDE_DIRS})
nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS simple.proto)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set_source_files_properties(${PROTO_SRCS} ${PROTO_HDRS}
PROPERTIES GENERATED TRUE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -g -O0")
add_executable(simple simple.c ${PROTO_SRCS} ${PROTO_HDRS})
$ protoc -o message.pb message.proto
$ python nanopb/generator/nanopb_generator.py message.pb
myMessage.proto定义成下面形式:
syntax = "proto3";
message myMessage {
repeated float myArray = 1
repeated float myVector = 2;
}
定义一个myMessage.options
#myMessage
myMessage.myArray max_count:10 fixed_count:true
myMessage.myVector max_count:10
生成的myMessage.pb.h文件
typedef struct _myMessage {
float myArray[10];
pb_size_t myVector_count;
float myVector[10];
} myMessage;
[1] https://stackoverflow.com/questions/46478988/unable-to-import-google-protobuf-python-module
[2] https://blog.csdn.net/shangsongwww/article/details/101066274