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