xref: /aosp_15_r20/external/protobuf/src/README.md (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1*1b3f573fSAndroid Build Coastguard WorkerProtocol Buffers - Google's data interchange format
2*1b3f573fSAndroid Build Coastguard Worker===================================================
3*1b3f573fSAndroid Build Coastguard Worker
4*1b3f573fSAndroid Build Coastguard WorkerCopyright 2008 Google Inc.
5*1b3f573fSAndroid Build Coastguard Worker
6*1b3f573fSAndroid Build Coastguard Workerhttps://developers.google.com/protocol-buffers/
7*1b3f573fSAndroid Build Coastguard Worker
8*1b3f573fSAndroid Build Coastguard WorkerC++ Installation - Unix
9*1b3f573fSAndroid Build Coastguard Worker-----------------------
10*1b3f573fSAndroid Build Coastguard Worker
11*1b3f573fSAndroid Build Coastguard WorkerTo build protobuf from source, the following tools are needed:
12*1b3f573fSAndroid Build Coastguard Worker
13*1b3f573fSAndroid Build Coastguard Worker  * autoconf
14*1b3f573fSAndroid Build Coastguard Worker  * automake
15*1b3f573fSAndroid Build Coastguard Worker  * libtool
16*1b3f573fSAndroid Build Coastguard Worker  * make
17*1b3f573fSAndroid Build Coastguard Worker  * g++
18*1b3f573fSAndroid Build Coastguard Worker  * unzip
19*1b3f573fSAndroid Build Coastguard Worker
20*1b3f573fSAndroid Build Coastguard WorkerOn Ubuntu/Debian, you can install them with:
21*1b3f573fSAndroid Build Coastguard Worker
22*1b3f573fSAndroid Build Coastguard Worker    sudo apt-get install autoconf automake libtool curl make g++ unzip
23*1b3f573fSAndroid Build Coastguard Worker
24*1b3f573fSAndroid Build Coastguard WorkerOn other platforms, please use the corresponding package managing tool to
25*1b3f573fSAndroid Build Coastguard Workerinstall them before proceeding.
26*1b3f573fSAndroid Build Coastguard Worker
27*1b3f573fSAndroid Build Coastguard WorkerTo get the source, download one of the release .tar.gz or .zip packages in the
28*1b3f573fSAndroid Build Coastguard Workerrelease page:
29*1b3f573fSAndroid Build Coastguard Worker
30*1b3f573fSAndroid Build Coastguard Worker    https://github.com/protocolbuffers/protobuf/releases/latest
31*1b3f573fSAndroid Build Coastguard Worker
32*1b3f573fSAndroid Build Coastguard WorkerFor example: if you only need C++, download `protobuf-cpp-[VERSION].tar.gz`; if
33*1b3f573fSAndroid Build Coastguard Workeryou need C++ and Java, download `protobuf-java-[VERSION].tar.gz` (every package
34*1b3f573fSAndroid Build Coastguard Workercontains C++ source already); if you need C++ and multiple other languages,
35*1b3f573fSAndroid Build Coastguard Workerdownload `protobuf-all-[VERSION].tar.gz`.
36*1b3f573fSAndroid Build Coastguard Worker
37*1b3f573fSAndroid Build Coastguard WorkerYou can also get the source by "git clone" our git repository. Make sure you
38*1b3f573fSAndroid Build Coastguard Workerhave also cloned the submodules and generated the configure script (skip this
39*1b3f573fSAndroid Build Coastguard Workerif you are using a release .tar.gz or .zip package):
40*1b3f573fSAndroid Build Coastguard Worker
41*1b3f573fSAndroid Build Coastguard Worker    git clone https://github.com/protocolbuffers/protobuf.git
42*1b3f573fSAndroid Build Coastguard Worker    cd protobuf
43*1b3f573fSAndroid Build Coastguard Worker    git submodule update --init --recursive
44*1b3f573fSAndroid Build Coastguard Worker    ./autogen.sh
45*1b3f573fSAndroid Build Coastguard Worker
46*1b3f573fSAndroid Build Coastguard WorkerTo build and install the C++ Protocol Buffer runtime and the Protocol
47*1b3f573fSAndroid Build Coastguard WorkerBuffer compiler (protoc) execute the following:
48*1b3f573fSAndroid Build Coastguard Worker
49*1b3f573fSAndroid Build Coastguard Worker
50*1b3f573fSAndroid Build Coastguard Worker     ./configure
51*1b3f573fSAndroid Build Coastguard Worker     make -j$(nproc) # $(nproc) ensures it uses all cores for compilation
52*1b3f573fSAndroid Build Coastguard Worker     make check
53*1b3f573fSAndroid Build Coastguard Worker     sudo make install
54*1b3f573fSAndroid Build Coastguard Worker     sudo ldconfig # refresh shared library cache.
55*1b3f573fSAndroid Build Coastguard Worker
56*1b3f573fSAndroid Build Coastguard WorkerIf "make check" fails, you can still install, but it is likely that
57*1b3f573fSAndroid Build Coastguard Workersome features of this library will not work correctly on your system.
58*1b3f573fSAndroid Build Coastguard WorkerProceed at your own risk.
59*1b3f573fSAndroid Build Coastguard Worker
60*1b3f573fSAndroid Build Coastguard WorkerFor advanced usage information on configure and make, please refer to the
61*1b3f573fSAndroid Build Coastguard Workerautoconf documentation:
62*1b3f573fSAndroid Build Coastguard Worker
63*1b3f573fSAndroid Build Coastguard Worker    http://www.gnu.org/software/autoconf/manual/autoconf.html#Running-configure-Scripts
64*1b3f573fSAndroid Build Coastguard Worker
65*1b3f573fSAndroid Build Coastguard Worker**Hint on install location**
66*1b3f573fSAndroid Build Coastguard Worker
67*1b3f573fSAndroid Build Coastguard WorkerBy default, the package will be installed to /usr/local.  However,
68*1b3f573fSAndroid Build Coastguard Workeron many platforms, /usr/local/lib is not part of LD_LIBRARY_PATH.
69*1b3f573fSAndroid Build Coastguard WorkerYou can add it, but it may be easier to just install to /usr
70*1b3f573fSAndroid Build Coastguard Workerinstead.  To do this, invoke configure as follows:
71*1b3f573fSAndroid Build Coastguard Worker
72*1b3f573fSAndroid Build Coastguard Worker    ./configure --prefix=/usr
73*1b3f573fSAndroid Build Coastguard Worker
74*1b3f573fSAndroid Build Coastguard WorkerIf you already built the package with a different prefix, make sure
75*1b3f573fSAndroid Build Coastguard Workerto run "make clean" before building again.
76*1b3f573fSAndroid Build Coastguard Worker
77*1b3f573fSAndroid Build Coastguard Worker**Compiling dependent packages**
78*1b3f573fSAndroid Build Coastguard Worker
79*1b3f573fSAndroid Build Coastguard WorkerTo compile a package that uses Protocol Buffers, you need to pass
80*1b3f573fSAndroid Build Coastguard Workervarious flags to your compiler and linker.  As of version 2.2.0,
81*1b3f573fSAndroid Build Coastguard WorkerProtocol Buffers integrates with pkg-config to manage this.  If you
82*1b3f573fSAndroid Build Coastguard Workerhave pkg-config installed, then you can invoke it to get a list of
83*1b3f573fSAndroid Build Coastguard Workerflags like so:
84*1b3f573fSAndroid Build Coastguard Worker
85*1b3f573fSAndroid Build Coastguard Worker
86*1b3f573fSAndroid Build Coastguard Worker    pkg-config --cflags protobuf         # print compiler flags
87*1b3f573fSAndroid Build Coastguard Worker    pkg-config --libs protobuf           # print linker flags
88*1b3f573fSAndroid Build Coastguard Worker    pkg-config --cflags --libs protobuf  # print both
89*1b3f573fSAndroid Build Coastguard Worker
90*1b3f573fSAndroid Build Coastguard Worker
91*1b3f573fSAndroid Build Coastguard WorkerFor example:
92*1b3f573fSAndroid Build Coastguard Worker
93*1b3f573fSAndroid Build Coastguard Worker    c++ my_program.cc my_proto.pb.cc `pkg-config --cflags --libs protobuf`
94*1b3f573fSAndroid Build Coastguard Worker
95*1b3f573fSAndroid Build Coastguard WorkerNote that packages written prior to the 2.2.0 release of Protocol
96*1b3f573fSAndroid Build Coastguard WorkerBuffers may not yet integrate with pkg-config to get flags, and may
97*1b3f573fSAndroid Build Coastguard Workernot pass the correct set of flags to correctly link against
98*1b3f573fSAndroid Build Coastguard Workerlibprotobuf.  If the package in question uses autoconf, you can
99*1b3f573fSAndroid Build Coastguard Workeroften fix the problem by invoking its configure script like:
100*1b3f573fSAndroid Build Coastguard Worker
101*1b3f573fSAndroid Build Coastguard Worker
102*1b3f573fSAndroid Build Coastguard Worker    configure CXXFLAGS="$(pkg-config --cflags protobuf)" \
103*1b3f573fSAndroid Build Coastguard Worker              LIBS="$(pkg-config --libs protobuf)"
104*1b3f573fSAndroid Build Coastguard Worker
105*1b3f573fSAndroid Build Coastguard WorkerThis will force it to use the correct flags.
106*1b3f573fSAndroid Build Coastguard Worker
107*1b3f573fSAndroid Build Coastguard WorkerIf you are writing an autoconf-based package that uses Protocol
108*1b3f573fSAndroid Build Coastguard WorkerBuffers, you should probably use the PKG_CHECK_MODULES macro in your
109*1b3f573fSAndroid Build Coastguard Workerconfigure script like:
110*1b3f573fSAndroid Build Coastguard Worker
111*1b3f573fSAndroid Build Coastguard Worker    PKG_CHECK_MODULES([protobuf], [protobuf])
112*1b3f573fSAndroid Build Coastguard Worker
113*1b3f573fSAndroid Build Coastguard WorkerSee the pkg-config man page for more info.
114*1b3f573fSAndroid Build Coastguard Worker
115*1b3f573fSAndroid Build Coastguard WorkerIf you only want protobuf-lite, substitute "protobuf-lite" in place
116*1b3f573fSAndroid Build Coastguard Workerof "protobuf" in these examples.
117*1b3f573fSAndroid Build Coastguard Worker
118*1b3f573fSAndroid Build Coastguard Worker**Note for Mac users**
119*1b3f573fSAndroid Build Coastguard Worker
120*1b3f573fSAndroid Build Coastguard WorkerFor a Mac system, Unix tools are not available by default. You will first need
121*1b3f573fSAndroid Build Coastguard Workerto install Xcode from the Mac AppStore and then run the following command from
122*1b3f573fSAndroid Build Coastguard Workera terminal:
123*1b3f573fSAndroid Build Coastguard Worker
124*1b3f573fSAndroid Build Coastguard Worker    sudo xcode-select --install
125*1b3f573fSAndroid Build Coastguard Worker
126*1b3f573fSAndroid Build Coastguard WorkerTo install Unix tools, you can install "port" following the instructions at
127*1b3f573fSAndroid Build Coastguard Workerhttps://www.macports.org . This will reside in /opt/local/bin/port for most
128*1b3f573fSAndroid Build Coastguard WorkerMac installations.
129*1b3f573fSAndroid Build Coastguard Worker
130*1b3f573fSAndroid Build Coastguard Worker    sudo /opt/local/bin/port install autoconf automake libtool
131*1b3f573fSAndroid Build Coastguard Worker
132*1b3f573fSAndroid Build Coastguard WorkerAlternative for Homebrew users:
133*1b3f573fSAndroid Build Coastguard Worker
134*1b3f573fSAndroid Build Coastguard Worker    brew install autoconf automake libtool
135*1b3f573fSAndroid Build Coastguard Worker
136*1b3f573fSAndroid Build Coastguard WorkerThen follow the Unix instructions above.
137*1b3f573fSAndroid Build Coastguard Worker
138*1b3f573fSAndroid Build Coastguard Worker**Note for cross-compiling**
139*1b3f573fSAndroid Build Coastguard Worker
140*1b3f573fSAndroid Build Coastguard WorkerThe makefiles normally invoke the protoc executable that they just
141*1b3f573fSAndroid Build Coastguard Workerbuilt in order to build tests.  When cross-compiling, the protoc
142*1b3f573fSAndroid Build Coastguard Workerexecutable may not be executable on the host machine.  In this case,
143*1b3f573fSAndroid Build Coastguard Workeryou must build a copy of protoc for the host machine first, then use
144*1b3f573fSAndroid Build Coastguard Workerthe --with-protoc option to tell configure to use it instead.  For
145*1b3f573fSAndroid Build Coastguard Workerexample:
146*1b3f573fSAndroid Build Coastguard Worker
147*1b3f573fSAndroid Build Coastguard Worker    ./configure --with-protoc=protoc
148*1b3f573fSAndroid Build Coastguard Worker
149*1b3f573fSAndroid Build Coastguard WorkerThis will use the installed protoc (found in your $PATH) instead of
150*1b3f573fSAndroid Build Coastguard Workertrying to execute the one built during the build process.  You can
151*1b3f573fSAndroid Build Coastguard Workeralso use an executable that hasn't been installed.  For example, if
152*1b3f573fSAndroid Build Coastguard Workeryou built the protobuf package for your host machine in ../host,
153*1b3f573fSAndroid Build Coastguard Workeryou might do:
154*1b3f573fSAndroid Build Coastguard Worker
155*1b3f573fSAndroid Build Coastguard Worker    ./configure --with-protoc=../host/src/protoc
156*1b3f573fSAndroid Build Coastguard Worker
157*1b3f573fSAndroid Build Coastguard WorkerEither way, you must make sure that the protoc executable you use
158*1b3f573fSAndroid Build Coastguard Workerhas the same version as the protobuf source code you are trying to
159*1b3f573fSAndroid Build Coastguard Workeruse it with.
160*1b3f573fSAndroid Build Coastguard Worker
161*1b3f573fSAndroid Build Coastguard Worker**Note for Solaris users**
162*1b3f573fSAndroid Build Coastguard Worker
163*1b3f573fSAndroid Build Coastguard WorkerSolaris 10 x86 has a bug that will make linking fail, complaining
164*1b3f573fSAndroid Build Coastguard Workerabout libstdc++.la being invalid.  We have included a work-around
165*1b3f573fSAndroid Build Coastguard Workerin this package.  To use the work-around, run configure as follows:
166*1b3f573fSAndroid Build Coastguard Worker
167*1b3f573fSAndroid Build Coastguard Worker    ./configure LDFLAGS=-L$PWD/src/solaris
168*1b3f573fSAndroid Build Coastguard Worker
169*1b3f573fSAndroid Build Coastguard WorkerSee src/solaris/libstdc++.la for more info on this bug.
170*1b3f573fSAndroid Build Coastguard Worker
171*1b3f573fSAndroid Build Coastguard Worker**Note for HP C++ Tru64 users**
172*1b3f573fSAndroid Build Coastguard Worker
173*1b3f573fSAndroid Build Coastguard WorkerTo compile invoke configure as follows:
174*1b3f573fSAndroid Build Coastguard Worker
175*1b3f573fSAndroid Build Coastguard Worker    ./configure CXXFLAGS="-O -std ansi -ieee -D__USE_STD_IOSTREAM"
176*1b3f573fSAndroid Build Coastguard Worker
177*1b3f573fSAndroid Build Coastguard WorkerAlso, you will need to use gmake instead of make.
178*1b3f573fSAndroid Build Coastguard Worker
179*1b3f573fSAndroid Build Coastguard Worker**Note for AIX users**
180*1b3f573fSAndroid Build Coastguard Worker
181*1b3f573fSAndroid Build Coastguard WorkerCompile using the IBM xlC C++ compiler as follows:
182*1b3f573fSAndroid Build Coastguard Worker
183*1b3f573fSAndroid Build Coastguard Worker    ./configure CXX=xlC
184*1b3f573fSAndroid Build Coastguard Worker
185*1b3f573fSAndroid Build Coastguard WorkerAlso, you will need to use GNU `make` (`gmake`) instead of AIX `make`.
186*1b3f573fSAndroid Build Coastguard Worker
187*1b3f573fSAndroid Build Coastguard WorkerC++ Installation - Windows
188*1b3f573fSAndroid Build Coastguard Worker--------------------------
189*1b3f573fSAndroid Build Coastguard Worker
190*1b3f573fSAndroid Build Coastguard WorkerIf you only need the protoc binary, you can download it from the release
191*1b3f573fSAndroid Build Coastguard Workerpage:
192*1b3f573fSAndroid Build Coastguard Worker
193*1b3f573fSAndroid Build Coastguard Worker    https://github.com/protocolbuffers/protobuf/releases/latest
194*1b3f573fSAndroid Build Coastguard Worker
195*1b3f573fSAndroid Build Coastguard WorkerIn the downloads section, download the zip file protoc-$VERSION-win32.zip.
196*1b3f573fSAndroid Build Coastguard WorkerIt contains the protoc binary as well as public proto files of protobuf
197*1b3f573fSAndroid Build Coastguard Workerlibrary.
198*1b3f573fSAndroid Build Coastguard Worker
199*1b3f573fSAndroid Build Coastguard WorkerProtobuf and its dependencies can be installed directly by using `vcpkg`:
200*1b3f573fSAndroid Build Coastguard Worker
201*1b3f573fSAndroid Build Coastguard Worker    >vcpkg install protobuf protobuf:x64-windows
202*1b3f573fSAndroid Build Coastguard Worker
203*1b3f573fSAndroid Build Coastguard WorkerIf zlib support is desired, you'll also need to install the zlib feature:
204*1b3f573fSAndroid Build Coastguard Worker
205*1b3f573fSAndroid Build Coastguard Worker    >vcpkg install protobuf[zlib] protobuf[zlib]:x64-windows
206*1b3f573fSAndroid Build Coastguard Worker
207*1b3f573fSAndroid Build Coastguard WorkerSee https://github.com/Microsoft/vcpkg for more information.
208*1b3f573fSAndroid Build Coastguard Worker
209*1b3f573fSAndroid Build Coastguard WorkerTo build from source using Microsoft Visual C++, see [cmake/README.md](../cmake/README.md).
210*1b3f573fSAndroid Build Coastguard Worker
211*1b3f573fSAndroid Build Coastguard WorkerTo build from source using Cygwin or MinGW, follow the Unix installation
212*1b3f573fSAndroid Build Coastguard Workerinstructions, above.
213*1b3f573fSAndroid Build Coastguard Worker
214*1b3f573fSAndroid Build Coastguard WorkerBinary Compatibility Warning
215*1b3f573fSAndroid Build Coastguard Worker----------------------------
216*1b3f573fSAndroid Build Coastguard Worker
217*1b3f573fSAndroid Build Coastguard WorkerDue to the nature of C++, it is unlikely that any two versions of the
218*1b3f573fSAndroid Build Coastguard WorkerProtocol Buffers C++ runtime libraries will have compatible ABIs.
219*1b3f573fSAndroid Build Coastguard WorkerThat is, if you linked an executable against an older version of
220*1b3f573fSAndroid Build Coastguard Workerlibprotobuf, it is unlikely to work with a newer version without
221*1b3f573fSAndroid Build Coastguard Workerre-compiling.  This problem, when it occurs, will normally be detected
222*1b3f573fSAndroid Build Coastguard Workerimmediately on startup of your app.  Still, you may want to consider
223*1b3f573fSAndroid Build Coastguard Workerusing static linkage.  You can configure this package to install
224*1b3f573fSAndroid Build Coastguard Workerstatic libraries only using:
225*1b3f573fSAndroid Build Coastguard Worker
226*1b3f573fSAndroid Build Coastguard Worker    ./configure --disable-shared
227*1b3f573fSAndroid Build Coastguard Worker
228*1b3f573fSAndroid Build Coastguard WorkerUsage
229*1b3f573fSAndroid Build Coastguard Worker-----
230*1b3f573fSAndroid Build Coastguard Worker
231*1b3f573fSAndroid Build Coastguard WorkerThe complete documentation for Protocol Buffers is available via the
232*1b3f573fSAndroid Build Coastguard Workerweb at:
233*1b3f573fSAndroid Build Coastguard Worker
234*1b3f573fSAndroid Build Coastguard Workerhttps://developers.google.com/protocol-buffers/
235