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