[ACCEPTED]-How to build Google's protobuf in Windows using MinGW?-protocol-buffers

Accepted answer
Score: 27

Here's what worked for me:

  1. You need to install 53 MSYS with mingw. This is a minimal unix-like 52 shell environment that lets you configure/make 51 most unix packages. Read the mingw docs 50 on how to install that (either with mingw-get 49 or the GUI installer).

  2. Once you have installed 48 MSYS, you should have a shortcut in your 47 start menu, named "MinGW Shell". That 46 opens a console with a bash.

  3. Extract the 45 source tarball to your MSYS home directory. I 44 have mingw installed in "D:\prog", so 43 the directory was "D:\prog\MinGW\msys\1.0\home\<username>". You 42 can tell your MSYS username from the shell 41 prompt. When done, you should have a directory 40 "D:\prog\MinGW\msys\1.0\home\<username>\protobuf-2.4.1".

  4. At 39 the shell prompt, change to the protobuf 38 directory:

    cd protobuf-2.4.1

  5. Run the configure script (note 37 the backquotes):

    ./configure --prefix=&#96;cd /mingw; pwd -W&#96;

    The --prefix paramater makes sure 36 protobuf is installed in the mingw directory 35 tree instead of the MSYS directories, so 34 you can build outside the MSYS shell (e.g. with 33 CodeBlocks...)

  6. Run make:

    make

  7. Install:

    make install

  8. That's it. You 32 should now be able to compile your project 31 with protobuf.
    You should be able to:

    • call protoc from your project/makefiles
    • #include <google/protobuf/message.h> etc.
    • link with -lprotobuf or -lprotobuf-lite

HTH
Peter

Edit: Bringing 30 this a bit more up to date. I tried setting 29 up a new PC with current versions of MinGW 28 and protobuf 2.5.0, and these are the problems 27 I had:

  1. There is no "MinGW Shell" shortcut 26 in the start menu.
    For some reason current 25 MinGW installations fail to install that.
    But 24 there is a msys.bat in <Mingw home>\msys\1.0 which brings up a console 23 with a bash. Create a shortcut to that batch 22 file somewhere.

  2. gcc does not work from the 21 MSYS shell.
    I had to run a post-installation 20 batch file manually and answer the questions 19 there. This sets up fstab entries that mount 18 the mingw directories in the MSYS environment.
    You 17 need to run <Mingw home>\msys\1.0\postinstall\pi.bat

  3. My Avira antivirus interfered 16 with the protobuf compilation.
    It complained 15 about the generated protoc.exe being a "TR/Crypt.XPACK.Gen" trojan 14 and blocked acces to that file, resulting 13 in a corrupted build.
    I got error messages 12 saying something like protoc:./.libs/lt-protoc.c:233: FATAL: couldn't find protoc. when trying to start 11 protoc.
    I had to disable the Avira realtime 10 scanner and make clean && make && make install again

Edit #2:

This post has 9 aged quite a bit, and mingw does not equal 8 mingw anymore. In this day and age, I would 7 rather recommend MSYS2 which comes with 6 a port of ArchLinux's pacman package manager, a 5 recent, better-working (c++11 std::thread 4 support!) mingw fork for both 32 and 64 3 bit, and a protobuf package that you just 2 need to install and be good.

Go here to download!

Hope 1 this helps!
Peter

Score: 1

In my case Peter's answer did not work completely, I 6 used the latest MinGW 4.8.1 + the MSys distribution 5 (selected both MSys packages in mingw-get).

My problem 4 was that the prefix didn't really work, I 3 could only find the files in C:\MinGW\msys\1.0\local . However, after 2 copying the bin / include / libs folders 1 to c:\mingw, the installation worked for me, too.

Score: 0

I had the same problem and i solved it by 6 building protocol buffers using boost build. That 5 worked fine, I can provide a jamfile for 4 protocol buffers.

What I still have problems 3 with though is to extend boost build so 2 it generates cpp source files from proto 1 files, but that is another story.

Score: 0

LATEST ANSWER

Here is an answer for the present-day versions 17 of protobuf source distribution, for using 16 protobuf for C++ language in Windows. (My 15 protobuf version is 21.4- libprotoc 3.21.4)

Referring to 14 the answer by @peter-remmers

Step 0: Download protobuf 13 zip file from release page. e.g. "protobuf-cpp-3.21.4.zip"

  • Extract it to a path where you want protobuf to be installed.
  • Add the "src" folder path to system's environment path variables. e.g. "C:\Path-To-Protobuf\protobuf-3.21.4\src"

Step 1: Download 12 & install msys2: https://www.msys2.org/

Make sure to to do 11 these:

  • pacman -Syu
  • Run "MSYS2 MSYS" from Start menu. Update the rest of the base packages with pacman -Syu
  • pacman -S --needed base-devel mingw-w64-x86_64-toolchain

Step 2: Add the mingw bin files' path to 10 system's environment variables.

  • e.g. it was "C:\Path-To-Msys2\msys2\mingw64\bin" and "C:\Path-To-Msys2\msys2\usr\bin"
  • Confirm by Checking the g++ version: g++ --version in the terminal (e.g. Mine is 12.1.0)

Step 3: Setting 9 Up C++ runtime with protobuf libraries:

  • So, back in msys2, install protobuf's libraries: pacman -S mingw-w64-x86_64-protobuf. Reference
  • Now, change the directory to path where protobuf is installed in your system : e.g. cd "C:\Path-To-Protobuf\protobuf-3.21.4"
  • Run the configure file of protobuf: ./configure
  • Run make (In case of any error with aclocal, etc. Try running pacman -S autoconf, then try again make)
  • Run make install

Step 4: That's 8 it. You should now be able to compile your 7 project with protobuf.

e.g. To compile a 6 .proto file using protoc to cpp code & header 5 files:

  • Use: protoc --cpp_out=$OUTDIR example.proto
  • Two files, a pb.cc & a pb.h file will be generated.
  • Write a cpp code to use that header file and create objects, populate data, etc.
  • Compile that cpp writer file from terminal/PowerShell like:

g++ -I "C:\Path-To-Protobuf\protobuf-3.21.4\src" "Path-To-Code\writer.cpp" "Path-To-Code\example.pb.cc" -o "Path-To-Code\writer.exe" -L "C:\Path-To-Protobuf\protobuf-3.21.4\src\.libs" -lprotobuf -pthread

("-pthread" is not really 4 important at the end I guess.)

NOTE (The 3 problem I had): Order of paths of e.g. mingw 2 in system's environment variables list matters 1 very much.

More Related questions