Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 1 | This directory contains cmake files that can be used to generate MSVC project |
| 2 | files in order to build protobuf on windows. You need to have cmake installed |
| 3 | on your computer before proceeding. |
| 4 | |
| 5 | Compiling and Installing |
| 6 | ======================== |
| 7 | |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 8 | 1. Check whether a gtest directory exists in the upper level directory. If |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 9 | you checkout the code from github via "git clone", this gtest directory |
| 10 | won't exist and you won't be able to build the tests described below. To |
| 11 | avoid this problem consider downloading one of the release tar balls which |
| 12 | contains gtest already and copying the gest directory from there to your |
| 13 | protobuf directory: |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 14 | |
| 15 | https://github.com/google/protobuf/releases |
| 16 | |
| 17 | 2. Use cmake to generate MSVC project files. Running the following commands |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 18 | in a command shell will generate project files for Visual Studio 2008 in |
| 19 | a sub-directory named "build". |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 20 | |
| 21 | $ cd path/to/protobuf/cmake |
| 22 | $ mkdir build |
| 23 | $ cd build |
| 24 | $ cmake -G "Visual Studio 9 2008" .. |
| 25 | |
| 26 | 3. Open the generated protobuf.sln file in Microsoft Visual Studio. |
| 27 | 4. Choose "Debug" or "Release" configuration as desired. |
| 28 | 5. From the Build menu, choose "Build Solution". Wait for compiling to finish. |
| 29 | 6. From a command shell, run tests.exe and lite-test.exe and check that all |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 30 | tests pass. Make sure you have changed the working directory to the output |
| 31 | directory because tests.exe will try to find and run test_plugin.exe |
| 32 | in the working directory. |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 33 | 7. Run extract_includes.bat to copy all the public headers into a separate |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 34 | "include" directory (under the top-level package directory). |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 35 | 8. Copy the contents of the include directory to wherever you want to put |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 36 | headers. |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 37 | 9. Copy protoc.exe wherever you put build tools (probably somewhere in your |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 38 | PATH). |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 39 | 10. Copy libprotobuf.lib, libprotobuf-lite.lib, and libprotoc.lib wherever you |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 40 | put libraries. |
| 41 | |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 42 | To avoid conflicts between the MSVC debug and release runtime libraries, when |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 43 | compiling a debug build of your application, you may need to link against a |
| 44 | debug build of libprotobuf.lib. Similarly, release builds should link against |
| 45 | release libs. |
| 46 | |
| 47 | DLLs vs. static linking |
| 48 | ======================= |
| 49 | |
| 50 | Static linking is now the default for the Protocol Buffer libraries. Due to |
| 51 | issues with Win32's use of a separate heap for each DLL, as well as binary |
| 52 | compatibility issues between different versions of MSVC's STL library, it is |
| 53 | recommended that you use static linkage only. However, it is possible to |
| 54 | build libprotobuf and libprotoc as DLLs if you really want. To do this, |
| 55 | do the following: |
| 56 | |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 57 | 1. Add an additional flag "-DBUILD_SHARED_LIBS=ON" when invoking cmake: |
| 58 | |
| 59 | $ cmake -G "Visual Studio 9 2008" -DBUILD_SHARED_LIBS=ON .. |
| 60 | |
| 61 | 2. Follow the same steps as described in the above section. |
| 62 | 3. When compiling your project, make sure to #define PROTOBUF_USE_DLLS. |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 63 | |
| 64 | When distributing your software to end users, we strongly recommend that you |
| 65 | do NOT install libprotobuf.dll or libprotoc.dll to any shared location. |
| 66 | Instead, keep these libraries next to your binaries, in your application's |
| 67 | own install directory. C++ makes it very difficult to maintain binary |
| 68 | compatibility between releases, so it is likely that future versions of these |
| 69 | libraries will *not* be usable as drop-in replacements. |
| 70 | |
| 71 | If your project is itself a DLL intended for use by third-party software, we |
| 72 | recommend that you do NOT expose protocol buffer objects in your library's |
| 73 | public interface, and that you statically link protocol buffers into your |
| 74 | library. |
| 75 | |
| 76 | ZLib support |
| 77 | ============ |
| 78 | |
| 79 | If you want to include GzipInputStream and GzipOutputStream |
| 80 | (google/protobuf/io/gzip_stream.h) in libprotobuf, you will need to do a few |
| 81 | additional steps: |
| 82 | |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 83 | 1. Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works. |
| 84 | 2. Make sure zlib's two headers are in your include path and that the .lib file |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 85 | is in your library path. You could place all three files directly into this |
| 86 | cmake directory to compile libprotobuf, but they need to be visible to |
| 87 | your own project as well, so you should probably just put them into the |
| 88 | VC shared icnlude and library directories. |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 89 | 3. Add flag "-DZLIB=ON" when invoking cmake: |
| 90 | |
| 91 | $ cmake -G "Visual Studio 9 2008" -DZLIB=ON .. |
| 92 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 93 | If it reports NOTFOUND for zlib_include or zlib_lib, you might haven't put |
| 94 | the headers or the .lib file in the right directory. |
| 95 | 4) Open the generated protobuf.sln file and build as usual. |
| 96 | |
| 97 | Notes on Compiler Warnings |
| 98 | ========================== |
| 99 | |
| 100 | The following warnings have been disabled while building the protobuf libraries |
| 101 | and compiler. You may have to disable some of them in your own project as |
| 102 | well, or live with them. |
| 103 | |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 104 | * C4018 - 'expression' : signed/unsigned mismatch |
| 105 | * C4146 - unary minus operator applied to unsigned type, result still unsigned |
| 106 | * C4244 - Conversion from 'type1' to 'type2', possible loss of data. |
| 107 | * C4251 - 'identifier' : class 'type' needs to have dll-interface to be used by |
| 108 | clients of class 'type2' |
| 109 | * C4267 - Conversion from 'size_t' to 'type', possible loss of data. |
| 110 | * C4305 - 'identifier' : truncation from 'type1' to 'type2' |
| 111 | * C4355 - 'this' : used in base member initializer list |
| 112 | * C4800 - 'type' : forcing value to bool 'true' or 'false' (performance warning) |
| 113 | * C4996 - 'function': was declared deprecated |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 114 | |
| 115 | C4251 is of particular note, if you are compiling the Protocol Buffer library |
| 116 | as a DLL (see previous section). The protocol buffer library uses templates in |
| 117 | its public interfaces. MSVC does not provide any reasonable way to export |
| 118 | template classes from a DLL. However, in practice, it appears that exporting |
| 119 | templates is not necessary anyway. Since the complete definition of any |
| 120 | template is available in the header files, anyone importing the DLL will just |
| 121 | end up compiling instances of the templates into their own binary. The |
| 122 | Protocol Buffer implementation does not rely on static template members being |
| 123 | unique, so there should be no problem with this, but MSVC prints warning |
| 124 | nevertheless. So, we disable it. Unfortunately, this warning will also be |
| 125 | produced when compiling code which merely uses protocol buffers, meaning you |
| 126 | may have to disable it in your code too. |
Feng Xiao | 2286ab3 | 2015-06-04 11:12:32 -0700 | [diff] [blame^] | 127 | |