mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
210 lines
13 KiB
Plaintext
210 lines
13 KiB
Plaintext
= Protobuf Code Generation
|
|
:toc:
|
|
:toc-placement!:
|
|
|
|
toc::[]
|
|
|
|
# Introduction
|
|
|
|
This example shows how to generate source files using https://github.com/google/protobuf[protobuf].
|
|
Protocol Buffers is a data serialization format from Google. A user provides a
|
|
`.proto` file with a description of the data. Then using the protobuf compiler, the proto file
|
|
can be translated into source code in a number of languages including C++.
|
|
|
|
The files in this tutorial are below:
|
|
|
|
```
|
|
$ tree
|
|
.
|
|
├── AddressBook.proto
|
|
├── CMakeLists.txt
|
|
├── main.cpp
|
|
```
|
|
|
|
* link:AddressBook.proto[] - proto file from main protocol buffer https://developers.google.com/protocol-buffers/docs/cpptutorial[example]
|
|
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
|
|
* link:main.cpp[] - The source file from the protobuf example.
|
|
|
|
# Requirements
|
|
|
|
This example requires the protocol buffers binary and libraries to be installed.
|
|
|
|
This can be installed on Ubuntu using
|
|
|
|
[source,bash]
|
|
----
|
|
sudo apt-get install protobuf-compiler libprotobuf-dev
|
|
----
|
|
|
|
# Concepts
|
|
|
|
## Exported Variables
|
|
|
|
The variables exported by the CMake protobuf package and used in this example include:
|
|
|
|
* `PROTOBUF_FOUND` - If Protocol Buffers is installed
|
|
* `PROTOBUF_INCLUDE_DIRS` - The protobuf header files
|
|
* `PROTOBUF_LIBRARIES` - The protobuf library
|
|
|
|
More variables are defined and can be found by examining the documentation at the
|
|
top of your `FindProtobuf.cmake` file.
|
|
|
|
## Generating Source
|
|
|
|
The protobuf CMake package includes a number of helper functions to make the
|
|
code generation easier. In this example we are generating C++ source and use
|
|
the following code:
|
|
|
|
[source,cmake]
|
|
----
|
|
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS AddressBook.proto)
|
|
----
|
|
|
|
The arguments are:
|
|
|
|
* PROTO_SRCS - Name of the variable that will store the .pb.cc files.
|
|
* PROTO_HDRS- Name of the variable that will store the .pb.h files.
|
|
* AddressBook.proto - The .proto file to generate code from.
|
|
|
|
## Generated Files
|
|
|
|
After the `PROTOBUF_GENERATE_CPP` function is called, you will have the above
|
|
mentioned variables available. These will be marked as the output to a custom command
|
|
which calls the protobuf compiler binary to generate them.
|
|
|
|
To then have the files generated you should add them to a library or executable.
|
|
For example:
|
|
|
|
[source,cmake]
|
|
----
|
|
add_executable(protobuf_example
|
|
main.cpp
|
|
${PROTO_SRCS}
|
|
${PROTO_HDRS})
|
|
----
|
|
|
|
This will cause the protobuf compiler to be called when you call `make` on that
|
|
executables target.
|
|
|
|
When changes are made to the .proto file, the associated source files will be
|
|
autogenerated again. However, if no changes are made to the .proto file and you re-run
|
|
make, then nothing will be done.
|
|
|
|
# Building the Example
|
|
|
|
[source,bash]
|
|
----
|
|
$ mkdir build
|
|
|
|
$ cd build/
|
|
|
|
$ cmake ..
|
|
-- The C compiler identification is GNU 4.8.4
|
|
-- The CXX compiler identification is GNU 4.8.4
|
|
-- Check for working C compiler: /usr/bin/cc
|
|
-- Check for working C compiler: /usr/bin/cc -- works
|
|
-- Detecting C compiler ABI info
|
|
-- Detecting C compiler ABI info - done
|
|
-- Check for working CXX compiler: /usr/bin/c++
|
|
-- Check for working CXX compiler: /usr/bin/c++ -- works
|
|
-- Detecting CXX compiler ABI info
|
|
-- Detecting CXX compiler ABI info - done
|
|
-- Looking for include file pthread.h
|
|
-- Looking for include file pthread.h - found
|
|
-- Looking for pthread_create
|
|
-- Looking for pthread_create - not found
|
|
-- Looking for pthread_create in pthreads
|
|
-- Looking for pthread_create in pthreads - not found
|
|
-- Looking for pthread_create in pthread
|
|
-- Looking for pthread_create in pthread - found
|
|
-- Found Threads: TRUE
|
|
-- Found PROTOBUF: /usr/lib/x86_64-linux-gnu/libprotobuf.so
|
|
protobuf found
|
|
PROTO_SRCS = /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.cc
|
|
PROTO_HDRS = /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.h
|
|
-- Configuring done
|
|
-- Generating done
|
|
-- Build files have been written to: /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build
|
|
|
|
$ ls
|
|
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
|
|
|
|
$ make VERBOSE=1
|
|
/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/03-code-generation/protobuf -B/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build --check-build-system CMakeFiles/Makefile.cmake 0
|
|
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/progress.marks
|
|
make -f CMakeFiles/Makefile2 all
|
|
make[1]: Entering directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
make -f CMakeFiles/protobuf_example.dir/build.make CMakeFiles/protobuf_example.dir/depend
|
|
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 1
|
|
[ 33%] Running C++ protocol buffer compiler on AddressBook.proto
|
|
/usr/bin/protoc --cpp_out /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build -I /home/matrim/workspace/cmake-examples/03-code-generation/protobuf /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/AddressBook.proto
|
|
cd /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/03-code-generation/protobuf /home/matrim/workspace/cmake-examples/03-code-generation/protobuf /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/DependInfo.cmake --color=
|
|
Dependee "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/DependInfo.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/depend.internal".
|
|
Dependee "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/depend.internal".
|
|
Scanning dependencies of target protobuf_example
|
|
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
make -f CMakeFiles/protobuf_example.dir/build.make CMakeFiles/protobuf_example.dir/build
|
|
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 2
|
|
[ 66%] Building CXX object CMakeFiles/protobuf_example.dir/main.cpp.o
|
|
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build -o CMakeFiles/protobuf_example.dir/main.cpp.o -c /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/main.cpp
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 3
|
|
[100%] Building CXX object CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.o
|
|
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build -o CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.o -c /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.cc
|
|
Linking CXX executable protobuf_example
|
|
/usr/bin/cmake -E cmake_link_script CMakeFiles/protobuf_example.dir/link.txt --verbose=1
|
|
/usr/bin/c++ CMakeFiles/protobuf_example.dir/main.cpp.o CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.o -o protobuf_example -rdynamic -lprotobuf -lpthread
|
|
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 1 2 3
|
|
[100%] Built target protobuf_example
|
|
make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 0
|
|
$ make VERBOSE=1
|
|
/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/03-code-generation/protobuf -B/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build --check-build-system CMakeFiles/Makefile.cmake 0
|
|
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/progress.marks
|
|
make -f CMakeFiles/Makefile2 all
|
|
make[1]: Entering directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
make -f CMakeFiles/protobuf_example.dir/build.make CMakeFiles/protobuf_example.dir/depend
|
|
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 1
|
|
[ 33%] Running C++ protocol buffer compiler on AddressBook.proto
|
|
/usr/bin/protoc --cpp_out /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build -I /home/matrim/workspace/cmake-examples/03-code-generation/protobuf /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/AddressBook.proto
|
|
cd /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/03-code-generation/protobuf /home/matrim/workspace/cmake-examples/03-code-generation/protobuf /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/DependInfo.cmake --color=
|
|
Dependee "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/DependInfo.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/depend.internal".
|
|
Dependee "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/depend.internal".
|
|
Scanning dependencies of target protobuf_example
|
|
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
make -f CMakeFiles/protobuf_example.dir/build.make CMakeFiles/protobuf_example.dir/build
|
|
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 2
|
|
[ 66%] Building CXX object CMakeFiles/protobuf_example.dir/main.cpp.o
|
|
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build -o CMakeFiles/protobuf_example.dir/main.cpp.o -c /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/main.cpp
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 3
|
|
[100%] Building CXX object CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.o
|
|
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build -o CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.o -c /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.cc
|
|
Linking CXX executable protobuf_example
|
|
/usr/bin/cmake -E cmake_link_script CMakeFiles/protobuf_example.dir/link.txt --verbose=1
|
|
/usr/bin/c++ CMakeFiles/protobuf_example.dir/main.cpp.o CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.o -o protobuf_example -rdynamic -lprotobuf -lpthread
|
|
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 1 2 3
|
|
[100%] Built target protobuf_example
|
|
make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build'
|
|
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 0
|
|
|
|
$ ls
|
|
AddressBook.pb.cc CMakeCache.txt cmake_install.cmake protobuf_example
|
|
AddressBook.pb.h CMakeFiles Makefile
|
|
|
|
$ ./protobuf_example test.db
|
|
test.db: File not found. Creating a new file.
|
|
Enter person ID number: 11
|
|
Enter name: John Doe
|
|
Enter email address (blank for none): wolly@sheep.ie
|
|
Enter a phone number (or leave blank to finish):
|
|
|
|
$ ls
|
|
AddressBook.pb.cc CMakeCache.txt cmake_install.cmake protobuf_example
|
|
AddressBook.pb.h CMakeFiles Makefile test.db
|
|
----
|