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.