Add simple example built with CMake
This commit is contained in:
committed by
Petteri Aimonen
parent
02bd49bc93
commit
7c00b90910
17
examples/cmake_simple/CMakeLists.txt
Normal file
17
examples/cmake_simple/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
cmake_minimum_required(VERSION 2.8.12)
|
||||||
|
project(NANOPB_CMAKE_SIMPLE C)
|
||||||
|
|
||||||
|
set(NANOPB_SRC_ROOT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/../..)
|
||||||
|
set(CMAKE_MODULE_PATH ${NANOPB_SRC_ROOT_FOLDER}/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})
|
||||||
|
#add_custom_target(generate_proto_sources DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})
|
||||||
|
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})
|
||||||
25
examples/cmake_simple/README.txt
Normal file
25
examples/cmake_simple/README.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
Nanopb example "simple" using CMake
|
||||||
|
=======================
|
||||||
|
|
||||||
|
This example is the same as the simple nanopb example but built using CMake.
|
||||||
|
|
||||||
|
Example usage
|
||||||
|
-------------
|
||||||
|
|
||||||
|
On Linux, create a build directory and then call cmake:
|
||||||
|
|
||||||
|
nanopb/examples/cmake_simple$ mkdir build
|
||||||
|
nanopb/examples/cmake_simple$ cd build/
|
||||||
|
nanopb/examples/cmake_simple/build$ cmake ..
|
||||||
|
nanopb/examples/cmake_simple/build$ make
|
||||||
|
|
||||||
|
After that, you can run it with the command: ./simple
|
||||||
|
|
||||||
|
#On other platforms, you first have to compile the protocol definition using
|
||||||
|
#the following command::
|
||||||
|
#
|
||||||
|
# ../../generator-bin/protoc --nanopb_out=. simple.proto
|
||||||
|
#
|
||||||
|
#After that, add the following four files to your project and compile:
|
||||||
|
#
|
||||||
|
# simple.c simple.pb.c pb_encode.c pb_decode.c
|
||||||
68
examples/cmake_simple/simple.c
Normal file
68
examples/cmake_simple/simple.c
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <pb_encode.h>
|
||||||
|
#include <pb_decode.h>
|
||||||
|
#include "simple.pb.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/* This is the buffer where we will store our message. */
|
||||||
|
uint8_t buffer[128];
|
||||||
|
size_t message_length;
|
||||||
|
bool status;
|
||||||
|
|
||||||
|
/* Encode our message */
|
||||||
|
{
|
||||||
|
/* Allocate space on the stack to store the message data.
|
||||||
|
*
|
||||||
|
* Nanopb generates simple struct definitions for all the messages.
|
||||||
|
* - check out the contents of simple.pb.h! */
|
||||||
|
SimpleMessage message;
|
||||||
|
|
||||||
|
/* Create a stream that will write to our buffer. */
|
||||||
|
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
/* Fill in the lucky number */
|
||||||
|
message.lucky_number = 13;
|
||||||
|
|
||||||
|
/* Now we are ready to encode the message! */
|
||||||
|
status = pb_encode(&stream, SimpleMessage_fields, &message);
|
||||||
|
message_length = stream.bytes_written;
|
||||||
|
|
||||||
|
/* Then just check for any errors.. */
|
||||||
|
if (!status)
|
||||||
|
{
|
||||||
|
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now we could transmit the message over network, store it in a file or
|
||||||
|
* wrap it to a pigeon's leg.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* But because we are lazy, we will just decode it immediately. */
|
||||||
|
|
||||||
|
{
|
||||||
|
/* Allocate space for the decoded message. */
|
||||||
|
SimpleMessage message;
|
||||||
|
|
||||||
|
/* Create a stream that reads from the buffer. */
|
||||||
|
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
|
||||||
|
|
||||||
|
/* Now we are ready to decode the message. */
|
||||||
|
status = pb_decode(&stream, SimpleMessage_fields, &message);
|
||||||
|
|
||||||
|
/* Check for errors... */
|
||||||
|
if (!status)
|
||||||
|
{
|
||||||
|
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Print the data contained in the message. */
|
||||||
|
printf("Your lucky number was %d!\n", message.lucky_number);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
7
examples/cmake_simple/simple.proto
Normal file
7
examples/cmake_simple/simple.proto
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// A very simple protocol definition, consisting of only
|
||||||
|
// one message.
|
||||||
|
|
||||||
|
message SimpleMessage {
|
||||||
|
required int32 lucky_number = 1;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user