blob: 02798b631a6e0da874c344ca34b980668d400663 [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 Xiao2286ab32015-06-04 11:12:32 -070081. Check whether a gtest directory exists in the upper level directory. If
Feng Xiao4333edb2015-05-31 02:28:34 -07009 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 Xiao2286ab32015-06-04 11:12:32 -070014
15 https://github.com/google/protobuf/releases
16
172. Use cmake to generate MSVC project files. Running the following commands
Feng Xiao4333edb2015-05-31 02:28:34 -070018 in a command shell will generate project files for Visual Studio 2008 in
19 a sub-directory named "build".
Feng Xiao2286ab32015-06-04 11:12:32 -070020
21 $ cd path/to/protobuf/cmake
22 $ mkdir build
23 $ cd build
24 $ cmake -G "Visual Studio 9 2008" ..
25
263. Open the generated protobuf.sln file in Microsoft Visual Studio.
274. Choose "Debug" or "Release" configuration as desired.
285. From the Build menu, choose "Build Solution". Wait for compiling to finish.
296. From a command shell, run tests.exe and lite-test.exe and check that all
Feng Xiao4333edb2015-05-31 02:28:34 -070030 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 Xiao2286ab32015-06-04 11:12:32 -0700337. Run extract_includes.bat to copy all the public headers into a separate
Feng Xiao4333edb2015-05-31 02:28:34 -070034 "include" directory (under the top-level package directory).
Feng Xiao2286ab32015-06-04 11:12:32 -0700358. Copy the contents of the include directory to wherever you want to put
Feng Xiao4333edb2015-05-31 02:28:34 -070036 headers.
Feng Xiao2286ab32015-06-04 11:12:32 -0700379. Copy protoc.exe wherever you put build tools (probably somewhere in your
Feng Xiao4333edb2015-05-31 02:28:34 -070038 PATH).
Feng Xiao2286ab32015-06-04 11:12:32 -07003910. Copy libprotobuf.lib, libprotobuf-lite.lib, and libprotoc.lib wherever you
Feng Xiao4333edb2015-05-31 02:28:34 -070040 put libraries.
41
Feng Xiao2286ab32015-06-04 11:12:32 -070042 To avoid conflicts between the MSVC debug and release runtime libraries, when
Feng Xiao4333edb2015-05-31 02:28:34 -070043 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
47DLLs vs. static linking
48=======================
49
50Static linking is now the default for the Protocol Buffer libraries. Due to
51issues with Win32's use of a separate heap for each DLL, as well as binary
52compatibility issues between different versions of MSVC's STL library, it is
53recommended that you use static linkage only. However, it is possible to
54build libprotobuf and libprotoc as DLLs if you really want. To do this,
55do the following:
56
Feng Xiao2286ab32015-06-04 11:12:32 -070057 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 Xiao4333edb2015-05-31 02:28:34 -070063
64When distributing your software to end users, we strongly recommend that you
65do NOT install libprotobuf.dll or libprotoc.dll to any shared location.
66Instead, keep these libraries next to your binaries, in your application's
67own install directory. C++ makes it very difficult to maintain binary
68compatibility between releases, so it is likely that future versions of these
69libraries will *not* be usable as drop-in replacements.
70
71If your project is itself a DLL intended for use by third-party software, we
72recommend that you do NOT expose protocol buffer objects in your library's
73public interface, and that you statically link protocol buffers into your
74library.
75
76ZLib support
77============
78
79If you want to include GzipInputStream and GzipOutputStream
80(google/protobuf/io/gzip_stream.h) in libprotobuf, you will need to do a few
81additional steps:
82
Feng Xiao2286ab32015-06-04 11:12:32 -0700831. Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works.
842. Make sure zlib's two headers are in your include path and that the .lib file
Feng Xiao4333edb2015-05-31 02:28:34 -070085 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 Xiao2286ab32015-06-04 11:12:32 -0700893. Add flag "-DZLIB=ON" when invoking cmake:
90
91 $ cmake -G "Visual Studio 9 2008" -DZLIB=ON ..
92
Feng Xiao4333edb2015-05-31 02:28:34 -070093 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.
954) Open the generated protobuf.sln file and build as usual.
96
97Notes on Compiler Warnings
98==========================
99
100The following warnings have been disabled while building the protobuf libraries
101and compiler. You may have to disable some of them in your own project as
102well, or live with them.
103
Feng Xiao2286ab32015-06-04 11:12:32 -0700104* 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 Xiao4333edb2015-05-31 02:28:34 -0700114
115C4251 is of particular note, if you are compiling the Protocol Buffer library
116as a DLL (see previous section). The protocol buffer library uses templates in
117its public interfaces. MSVC does not provide any reasonable way to export
118template classes from a DLL. However, in practice, it appears that exporting
119templates is not necessary anyway. Since the complete definition of any
120template is available in the header files, anyone importing the DLL will just
121end up compiling instances of the templates into their own binary. The
122Protocol Buffer implementation does not rely on static template members being
123unique, so there should be no problem with this, but MSVC prints warning
124nevertheless. So, we disable it. Unfortunately, this warning will also be
125produced when compiling code which merely uses protocol buffers, meaning you
126may have to disable it in your code too.
Feng Xiao2286ab32015-06-04 11:12:32 -0700127