先clone最新稳定版本源码
目前v33.5版本 自己选择心仪版本
git clone -b v33.5 https://github.com/protocolbuffers/protobuf.git
进入protobuf源码目录
cd protobuf
更新依赖库 递归全部
git submodule update --init --recursive
Win11
执行构建 vs2026 安装目录自己更换 后面作用:关闭测试 编译为静态库
cmake -S . -B build-vs -G "Visual Studio 18 2026" -A x64 -DCMAKE_INSTALL_PREFIX=D:/local/protobuf -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF
开始编译 --parallel 12 //多线程 msvc也可以-- /m:12
cmake --build build-vs --config Release --parallel 12
编译成功后 安装
cmake --install build-vs --config Release
Ubuntu24.04 (如果是wsl+Ubuntu24.04 如果win11环境有mingw或者msys2等gnu环境 请先清除)
根据官网提示安装好依赖
sudo apt update
sudo apt install -y \
build-essential \
cmake \
git \
autoconf \
automake \
libtool \
pkg-config \
curl \
unzip
开始构建
mkdir build && cd build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-Dprotobuf_BUILD_TESTS=OFF \
-Dprotobuf_BUILD_SHARED_LIBS=ON
make -j$(nproc)
sudo make install
sudo ldconfig
验证是否成功
protoc --version
输出类似
libprotoc 33.5
库路径
ls /usr/local/lib | grep proto
ls /usr/local/include/google/protobuf
编译lua-protobuf
先clone最新源码
git clone https://github.com/starwing/lua-protobuf.git
进入lua-protobuf目录
cd lua-protobuf
先安装好lua环境源文件和编译好的库文件 !!!
Ubuntu24.04
编译
gcc -O3 -shared -fPIC -I "$LUA_HEADERS" pb.c -o pb.so
Win11
先创建 lua-protobuf.slnx(vs2026解决方案)
添加pb.c和pb.h 添加lua源文件目录 重点: 预编译添加 Lua_BUILD_AS_DLL
cl /O2 /LD /Fepb.dll /I "$LUA_HEADERS" /DLUA_BUILD_AS_DLL pb.c "$LUA_LIBS"
测试一下
lua -e "require('pb'); print('pb ok')"
Enjoy~。~