1This directory contains *CMake* files that can be used to build protobuf. 2 3You need to have [CMake](http://www.cmake.org), 4[Git](http://git-scm.com), and [Abseil](https://github.com/abseil/abseil-cpp) 5installed on your computer before proceeding. We currently support CMake 3.5 6and newer on both [Windows](#windows-builds) and [Linux](#linux-builds). 7 8Most of the instructions will be given using CMake's command-line interface, but 9the same actions can be performed using appropriate GUI tools. 10 11# CMake Flags 12 13## C++ Version 14 15By default, CMake will use whatever C++ version is the system default. Since 16protobuf requires C++14 or newer, sometimes you will need to explicitly override 17this. For example, the following: 18 19``` 20cmake . -DCMAKE_CXX_STANDARD=14 21cmake --build . 22``` 23 24will build protobuf using C++14 (see [CXX_STANDARD](https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html#prop_tgt:CXX_STANDARD){.external} for all available options). 25 26# Windows Builds 27 28On Windows, you can build the project from *Command Prompt* and using an 29*Visual Studio* IDE. You will also need to have 30[Visual Studio](https://www.visualstudio.com) installed on your computer before 31proceeding. 32 33## Environment Setup 34 35Open the appropriate *Command Prompt* from the *Start* menu. 36 37For example *x86 Native Tools Command Prompt for VS 2019*: 38 39 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional> 40 41Change to your working directory: 42 43 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional>cd C:\Path\to 44 C:\Path\to> 45 46Where *C:\Path\to* is path to your real working directory. 47 48Create a folder where protobuf headers/libraries/binaries will be installed after built: 49 50 C:\Path\to>mkdir install 51 52If *cmake* command is not available from *Command Prompt*, add it to system *PATH* variable: 53 54 C:\Path\to>set PATH=%PATH%;C:\Program Files (x86)\CMake\bin 55 56If *git* command is not available from *Command Prompt*, add it to system *PATH* variable: 57 58 C:\Path\to>set PATH=%PATH%;C:\Program Files\Git\cmd 59 60Optionally, you will want to download [ninja](https://ninja-build.org/) and add it to your *PATH* variable. 61 62 C:\Path\to>set PATH=%PATH%;C:\tools\ninja 63 64Good. Now you are ready to continue. 65 66## Getting Sources 67 68You can get the latest stable source packages from the release page: 69 70 https://github.com/protocolbuffers/protobuf/releases/latest 71 72Or you can use git to clone from protobuf git repository. 73 74 C:\Path\to> mkdir src & cd src 75 C:\Path\to\src> git clone -b [release_tag] https://github.com/protocolbuffers/protobuf.git 76 77Where *[release_tag]* is a git tag like *v3.0.0-beta-1* or a branch name like *main* 78if you want to get the latest code. 79 80Go to the project folder: 81 82 C:\Path\to\src> cd protobuf 83 C:\Path\to\src\protobuf> 84 85Remember to update any submodules if you are using git clone (you can skip this 86step if you are using a release .tar.gz or .zip package): 87 88```console 89C:\Path\to\src\protobuf> git submodule update --init --recursive 90``` 91 92Good. Now you are ready for *CMake* configuration. 93 94## CMake Configuration 95 96*CMake* supports a lot of different 97[generators](http://www.cmake.org/cmake/help/latest/manual/cmake-generators.7.html) 98for various native build systems. 99 100Of most interest to Windows programmers are the following: 101 102* [Visual Studio](http://www.cmake.org/cmake/help/latest/manual/cmake-generators.7.html#visual-studio-generators) 103 This generates a Visual Studio solution for the project. 104 105* [Ninja](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#ninja-generator) 106 This uses the external tool [Ninja](https://ninja-build.org/) to build. It is the fastest solution available. 107 108Note that as of Visual Studio 2015, Visual Studio includes 109[support for opening directly CMake-based projects](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio). 110 111It is considered good practice not to build CMake projects in the source tree but in a separate folder. 112 113Create a temporary *build* folder and change your working directory to it: 114 115 mkdir C:\Path\to\build\protobuf 116 cd C:\Path\to\build\protobuf 117 C:\Path\to\build\protobuf> 118 119During configuration you will also be specifying where CMake should expect to 120find your Abseil installation. To do so, first set `-Dprotobuf_ABSL_PROVIDER=package` 121and then set `-DCMAKE_PREFIX_PATH` to the path where you installed Abseil. 122 123For example: 124 125```console 126C:\Path\to\build\protobuf> cmake -S. -Bcmake-out \ 127 -DCMAKE_INSTALL_PREFIX=/tmp/protobuf \ 128 -DCMAKE_CXX_STANDARD=14 \ 129 -Dprotobuf_ABSL_PROVIDER=package \ 130 -DCMAKE_PREFIX_PATH=/tmp/absl # Path to where I installed Abseil 131``` 132 133The *Makefile* and *Ninja* generators can build the project in only one configuration, so you need to build 134a separate folder for each configuration. 135 136To use *Debug* configuration using *Ninja*: 137 138 C:\Path\to\build\protobuf>mkdir debug & cd debug 139 C:\Path\to\build\protobuf\debug>cmake -G "Ninja" ^ 140 -DCMAKE_BUILD_TYPE=Debug ^ 141 -DCMAKE_INSTALL_PREFIX=C:\Path\to\install ^ 142 C:\Path\to\src\protobuf 143 144It will generate *Ninja* build scripts in current directory. 145 146The *Visual Studio* generator is multi-configuration: it will generate a single *.sln* file that can be used for both *Debug* and *Release*: 147 148 C:\Path\to\build\protobuf>mkdir solution & cd solution 149 C:\Path\to\build\protobuf\solution>cmake -G "Visual Studio 16 2019" ^ 150 -DCMAKE_INSTALL_PREFIX=C:\Path\to\install ^ 151 C:\Path\to\src\protobuf 152 153It will generate *Visual Studio* solution file *protobuf.sln* in current directory. 154 155### Unit Tests 156 157Unit tests are being built along with the rest of protobuf. The unit tests require Google Mock (now a part of Google Test). 158 159A copy of [Google Test](https://github.com/google/googletest) is included as a Git submodule in the `third-party/googletest` folder. 160(You do need to initialize the Git submodules as explained above.) 161 162Alternately, you may want to use protobuf in a larger set-up, you may want to use that standard CMake approach where 163you build and install a shared copy of Google Test. 164 165After you've built and installed your Google Test copy, you need add the following definition to your *cmake* command line 166during the configuration step: `-Dprotobuf_USE_EXTERNAL_GTEST=ON`. 167This will cause the standard CMake `find_package(GTest REQUIRED)` to be used. 168 169[find_package](https://cmake.org/cmake/help/latest/command/find_package.html) will search in a default location, 170which on Windows is *C:\Program Files*. This is most likely not what you want. You will want instead to search for 171Google Test in your project's root directory (i.e. the same directory you've passed to `CMAKE_INSTALL_PREFIX` when 172building Google Test). For this, you need to set the `CMAKE_PREFIX_PATH` CMake variable. (There are other ways in CMake, 173see the [manual](https://cmake.org/cmake/help/latest/command/find_package.html) for details.) 174 175For example: 176 177 C:\Path\to\build\protobuf>mkdir solution & cd solution 178 C:\Path\to\build\protobuf\solution>cmake -G "Visual Studio 16 2019" ^ 179 -DCMAKE_INSTALL_PREFIX=C:\Path\to\install ^ 180 -DCMAKE_PREFIX_PATH=C:\Path\to\my_big_project ^ 181 -Dprotobuf_USE_EXTERNAL_GTEST=ON ^ 182 C:\Path\to\src\protobuf 183 184In most cases, `CMAKE_PREFIX_PATH` and `CMAKE_INSTALL_PREFIX` will point to the same directory. 185 186To disable testing completely, you need to add the following argument to you *cmake* command line: `-Dprotobuf_BUILD_TESTS=OFF`. 187 188For example: 189 190 C:\Path\to\build\protobuf\solution>cmake -G "Visual Studio 16 2019" ^ 191 -DCMAKE_INSTALL_PREFIX=C:\Path\to\install ^ 192 -Dprotobuf_BUILD_TESTS=OFF ^ 193 C:\Path\to\src\protobuf 194 195## Compiling 196 197The standard way to compile a *CMake* project is `cmake --build <directory>`. 198 199 200Note that if your generator supports multiple configurations, you will probably want to specify which one to build: 201 202 cmake --build C:\Path\to\build\protobuf\solution --config Release 203 204You can also run directly the build tool you've configured: 205 206 C:\Path\to\build\protobuf\debug>ninja 207 208And wait for the compilation to finish. 209 210If you prefer to use the IDE: 211 212 * Open the generated protobuf.sln file in Microsoft Visual Studio. 213 * Choose "Debug" or "Release" configuration as desired. 214 * From the Build menu, choose "Build Solution". 215 216And wait for the compilation to finish. 217 218## Testing 219 220To run unit-tests, first you must compile protobuf as described above. 221Then run: 222 223 C:\Path\to\protobuf\cmake\build\release>ctest --progress --output-on-failure 224 225You can also build the `check` target (not idiomatic CMake usage, though): 226 227 C:\Path\to\protobuf\cmake\build\release>cmake --build . --target check 228 229or 230 231 C:\Path\to\build\protobuf\release>ninja check 232 233You can also build project *check* from Visual Studio solution. 234Yes, it may sound strange, but it works. 235 236You should see output similar to: 237 238 Running main() from gmock_main.cc 239 [==========] Running 1546 tests from 165 test cases. 240 241 ... 242 243 [==========] 1546 tests from 165 test cases ran. (2529 ms total) 244 [ PASSED ] 1546 tests. 245 246To run specific tests, you need to pass some command line arguments to the test program itself: 247 248 C:\Path\to\build\protobuf\release>tests.exe --gtest_filter=AnyTest* 249 Running main() from gmock_main.cc 250 Note: Google Test filter = AnyTest* 251 [==========] Running 3 tests from 1 test case. 252 [----------] Global test environment set-up. 253 [----------] 3 tests from AnyTest 254 [ RUN ] AnyTest.TestPackAndUnpack 255 [ OK ] AnyTest.TestPackAndUnpack (0 ms) 256 [ RUN ] AnyTest.TestPackAndUnpackAny 257 [ OK ] AnyTest.TestPackAndUnpackAny (0 ms) 258 [ RUN ] AnyTest.TestIs 259 [ OK ] AnyTest.TestIs (0 ms) 260 [----------] 3 tests from AnyTest (1 ms total) 261 262 [----------] Global test environment tear-down 263 [==========] 3 tests from 1 test case ran. (2 ms total) 264 [ PASSED ] 3 tests. 265 266Note that the tests must be run from the source folder. 267 268If all tests are passed, safely continue. 269 270## Installing 271 272To install protobuf to the *install* folder you've specified in the configuration step, you need to build the `install` target: 273 274 cmake --build C:\Path\to\build\protobuf\solution --config Release --target install 275 276Or if you prefer: 277 278 C:\Path\to\build\protobuf\debug>ninja install 279 280You can also build project *INSTALL* from Visual Studio solution. 281It sounds not so strange and it works. 282 283This will create the following folders under the *install* location: 284 * bin - that contains protobuf *protoc.exe* compiler; 285 * include - that contains C++ headers and protobuf *.proto files; 286 * lib - that contains linking libraries and *CMake* configuration files for *protobuf* package. 287 288Now you can if needed: 289 * Copy the contents of the include directory to wherever you want to put headers. 290 * Copy protoc.exe wherever you put build tools (probably somewhere in your PATH). 291 * Copy linking libraries libprotobuf[d].lib, libprotobuf-lite[d].lib, and libprotoc[d].lib wherever you put libraries. 292 293To avoid conflicts between the MSVC debug and release runtime libraries, when 294compiling a debug build of your application, you may need to link against a 295debug build of libprotobufd.lib with "d" postfix. Similarly, release builds should link against 296release libprotobuf.lib library. 297 298## DLLs vs. static linking 299 300Static linking is now the default for the Protocol Buffer libraries. Due to 301issues with Win32's use of a separate heap for each DLL, as well as binary 302compatibility issues between different versions of MSVC's STL library, it is 303recommended that you use static linkage only. However, it is possible to 304build libprotobuf and libprotoc as DLLs if you really want. To do this, 305do the following: 306 307 * Add an additional flag `-Dprotobuf_BUILD_SHARED_LIBS=ON` when invoking cmake 308 * Follow the same steps as described in the above section. 309 * When compiling your project, make sure to `#define PROTOBUF_USE_DLLS`. 310 311When distributing your software to end users, we strongly recommend that you 312do NOT install libprotobuf.dll or libprotoc.dll to any shared location. 313Instead, keep these libraries next to your binaries, in your application's 314own install directory. C++ makes it very difficult to maintain binary 315compatibility between releases, so it is likely that future versions of these 316libraries will *not* be usable as drop-in replacements. 317 318If your project is itself a DLL intended for use by third-party software, we 319recommend that you do NOT expose protocol buffer objects in your library's 320public interface, and that you statically link protocol buffers into your 321library. 322 323## ZLib support 324 325If you want to include GzipInputStream and GzipOutputStream 326(google/protobuf/io/gzip_stream.h) in libprotobuf, you will need to do a few 327additional steps. 328 329Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works. 330You need prepare it: 331 332 * Make sure zlib's two headers are in your `C:\Path\to\install\include` path 333 * Make sure zlib's linking libraries (*.lib file) is in your 334 `C:\Path\to\install\lib` library path. 335 336You can also compile it from source by yourself. 337 338Getting sources: 339 340 C:\Path\to\src>git clone -b v1.2.8 https://github.com/madler/zlib.git 341 C:\Path\to\src>cd zlib 342 343Compiling and Installing: 344 345 C:\Path\to\src\zlib>mkdir C:\Path\to\build\zlib & cd C:\Path\to\build\zlib 346 C:\Path\to\build\zlib>mkdir release & cd release 347 C:\Path\to\build\zlib\release>cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release ^ 348 -DCMAKE_INSTALL_PREFIX=C:\Path\to\install C:\Path\to\src\zlib 349 C:\Path\to\src\zlib\build\release>cmake --build . --target install 350 351You can make *debug* version or use *Visual Studio* generator also as before for the 352protobuf project. 353 354Now add *bin* folder from *install* to system *PATH*: 355 356 C:\Path\to>set PATH=%PATH%;C:\Path\to\install\bin 357 358You need reconfigure protobuf with flag `-Dprotobuf_WITH_ZLIB=ON` when invoking cmake. 359 360Note that if you have compiled ZLIB yourself, as stated above, 361further disable the option `-Dprotobuf_MSVC_STATIC_RUNTIME=OFF`. 362 363If it reports NOTFOUND for zlib_include or zlib_lib, you might haven't put 364the headers or the .lib file in the right directory. 365 366If you already have ZLIB library and headers at some other location on your system then alternatively you can define following configuration flags to locate them: 367 368 -DZLIB_INCLUDE_DIR=<path to dir containing zlib headers> 369 -DZLIB_LIB=<path to dir containing zlib> 370 371Build and testing protobuf as usual. 372 373## Notes on Compiler Warnings 374 375The following warnings have been disabled while building the protobuf libraries 376and compiler. You may have to disable some of them in your own project as 377well, or live with them. 378 379* C4244 - Conversion from 'type1' to 'type2', possible loss of data. 380* C4251 - 'identifier' : class 'type' needs to have dll-interface to be used by 381 clients of class 'type2' 382* C4267 - Conversion from 'size_t' to 'type', possible loss of data. 383* C4305 - 'identifier' : truncation from 'type1' to 'type2' 384* C4355 - 'this' : used in base member initializer list 385* C4800 - 'type' : forcing value to bool 'true' or 'false' (performance warning) 386* C4996 - 'function': was declared deprecated 387 388C4251 is of particular note, if you are compiling the Protocol Buffer library 389as a DLL (see previous section). The protocol buffer library uses templates in 390its public interfaces. MSVC does not provide any reasonable way to export 391template classes from a DLL. However, in practice, it appears that exporting 392templates is not necessary anyway. Since the complete definition of any 393template is available in the header files, anyone importing the DLL will just 394end up compiling instances of the templates into their own binary. The 395Protocol Buffer implementation does not rely on static template members being 396unique, so there should be no problem with this, but MSVC prints warning 397nevertheless. So, we disable it. Unfortunately, this warning will also be 398produced when compiling code which merely uses protocol buffers, meaning you 399may have to disable it in your code too. 400 401# Linux Builds 402 403Building with CMake works very similarly on Linux. Instead of Visual Studio, 404you will need to have gcc or clang installed to handle the C++ builds. CMake 405will generate Makefiles by default, but can also be configured to use Ninja. To 406build Protobuf, you will need to run (from the source directory): 407 408 cmake . 409 cmake --build . --parallel 10 410 411Protobuf can be tested and installed with CMake: 412 413 ctest --verbose 414 sudo cmake --install . 415 416or directly with the generated Makefiles: 417 418 make VERBOSE=1 test 419 sudo make install 420