Rdhafidh's blog

Building protobuf using MinGW on Windows

Published on 2019-03-01 11:44:07 by



This is small tips on how to build Google protobuf on windows using mingw 8.1. You can skip from 
reading this tutorial and download from their binaries release instead for convenience.
Before digging into deeper compilation steps, please make sure that you had already installed of all the following tools:

  1. MinGW 8.x 
  2. CMake tools for windows.
  3. A mingw32-make.exe for windows. (Normally this tool is bundled from either MinGW zip download or it's official installer)

If all of above tools is already present, then we can continue to our build process.

  1.  Create windows batch file 

set CMAKEPATH=C:\cmake\bin 
set MINGWPATH=C:\mingw32 
set CC=gcc 
set CXX=g++ 
SET INCLUDE=%MINGWPATH%\include;%MINGWPATH%\i686-w64-mingw32\include;%MINGWPATH%\x86_64-w64-mingw32\include;%MINGWPATH%\opt\include 
SET LIB=%MINGWPATH%\lib;%MINGWPATH%\i686-w64-mingw32\lib;%MINGWPATH%\x86_64-w64-mingw32\lib;%MINGWPATH%\opt\lib 
SET PATH=%PATH%;%MINGWPATH%\bin;%MINGWPATH%\i686-w64-mingw32\bin;%MINGWPATH%\x86_64-w64-mingw32\bin;%CMAKEPATH%;%MINGWPATH%\opt\bin 

and save it to c:\windows\mingw8.bat
  1. Clone protobuf repo.
git clone https://github.com/protocolbuffers/protobuf/
  1. Open command prompt and type mingw8. Enter following commands :
cd protobuf && mkdir ..\build && cmake.exe -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%MINGWPATH%\opt -G "MinGW Makefiles" ..\protobuf\cmake && mingw32-make
  1. You'll be greeted with following errors:

In file included from C:\mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:59,
                 from C:\mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/algorithm:62,
                 from d:/git/protobuf/src/google/protobuf/stubs/common.h:38,
                 from d:/git/protobuf/src/google/protobuf/arena_impl.h:39,
                 from d:/git/protobuf/src/google/protobuf/arena.h:51,
                 from D:\masteraplikasi\transferh11nov\protobufgit\protobuf\src\google\protobuf\arena.cc:31:
	C:\mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
		#inlude_next 
               ^~~~~~~~~~
	compilation terminated.
	mingw32-make[2]: *** [CMakeFiles/libprotobuf.dir/D_/git/protobuf/src/google/protobuf/arena.cc.obj] Error 1
	mingw32-make[1]: *** [CMakeFiles/libprotobuf.dir/all] Error 2
	mingw32-make: *** [all] Error 2

      The problem is that gcc treats -isystem differently than usual -I flag. So, to fix that we make sure append following additional cmake command 


-DCMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=%MINGWPATH%\i686-w64-mingw32\include

for 32 build and for 64 build is like follows :


-DCMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=%MINGWPATH%\x86_64-w64-mingw32\include

At the end, the following command is appropriate for 32 build:


cmake.exe -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=%MINGWPATH%\i686-w64-mingw32\include -DCMAKE_INSTALL_PREFIX=%MINGWPATH%\opt ..\protobuf\cmake  -G "MinGW Makefiles" && mingw32-make && mingw32-make install


I hope you enjoy it, thanks for reading.

 

Leave comment