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