Alex
Alex

Reputation: 1469

How to build Google's protobuf in Windows using MinGW?

I'm using Codeblocks as my IDE with MingGW. I'm trying to use google protocol buffers, but I'm having trouble building the protobuf.

The readme file for protobuf says:

If you are using Cygwin or MinGW, follow the Unix installation instructions, above.

The Unix instructions says:

To build and install the C++ Protocol Buffer runtime and the Protocol Buffer compiler (protoc) execute the following: $ ./configure $ make $ make check $ make install

I don't know how to perform these in Windows because "configure" is a Unix script and I don't know how to execute it, or the rest of the commands.

Can someone explain in more detail how I can build protobuf using MinGW on Windows?

Upvotes: 23

Views: 26682

Answers (4)

Kartikeya
Kartikeya

Reputation: 838

LATEST ANSWER

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

Referring to the answer by @peter-remmers

Step 0: Download protobuf 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 & install msys2: https://www.msys2.org/

Make sure to to do 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 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 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 it. You should now be able to compile your project with protobuf.

e.g. To compile a .proto file using protoc to cpp code & header 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 important at the end I guess.)

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

Upvotes: 0

Peter Remmers
Peter Remmers

Reputation: 1293

Here's what worked for me:

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

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

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

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

    cd protobuf-2.4.1

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

    ./configure --prefix=`cd /mingw; pwd -W`

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

  6. Run make:

    make

  7. Install:

    make install

  8. That's it. You should now be able to compile your project 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 this a bit more up to date. I tried setting up a new PC with current versions of MinGW and protobuf 2.5.0, and these are the problems I had:

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

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

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

Edit #2:

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

Go here to download!

Hope this helps!
Peter

Upvotes: 27

FourtyTwo
FourtyTwo

Reputation: 1751

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

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

Upvotes: 1

Jan Rosendahl
Jan Rosendahl

Reputation: 1

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

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

Upvotes: 0

Related Questions