protobuf2.6.1-全平台编译

环境:

工具 版本
macOS 11.2.3
Xcode 12.4(12D4e)
protobuf 2.6.1

一、首先 生成protoc – 编译Macos版本

1
2
3
4
5
6
7
8
9
make clean
./configure
make
make check
make install

# 查看是否正确安装
which protoc
protoc --version

二、编译ios版本

1、新建buid_script/build-ios.sh,并将以下写入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# 解决-fembed-bitcode与-bind_at_load冲突,10.0及以上不在使用编译选项bind_at_load
export MACOSX_DEPLOYMENT_TARGET="10.4"
cd ..

build_dir=`pwd`/build/ios
darwin=darwin`uname -r`
protoc=`which protoc`
isysroot=`xcrun --sdk iphoneos --show-sdk-path`

# 如果不需要支持bitcode 删除 -fembed-bitcode 即可
# 另如果报 make_pair 未定义,检查编译选项中是否指定了 -std=c++11 这里不需要再指定
cflags="-Wno-unused-local-typedef -Wno-unused-function -DNDEBUG -g -O0 -pipe -fPIC -fcxx-exceptions -fembed-bitcode"
cxxflags="$cflags -stdlib=libc++"

mkdir -p $build_dir/arch
mkdir -p $build_dir/lib

# 清空中间产物
make distclean

./configure \
--build=x86_64-apple-$darwin \
--host=arm \
--with-protoc=$protoc \
--disable-shared \
--prefix=$build_dir \
--exec-prefix=$build_dir/arch/arm64 \
"CC=clang" \
"CFLAGS=$cflags -miphoneos-version-min=9.0 -arch arm64 -isysroot $isysroot" \
"CXX=clang" \
"CXXFLAGS=$cxxflags -miphoneos-version-min=9.0 -arch arm64 -isysroot $isysroot" \
LDFLAGS="-arch arm64 -miphoneos-version-min=9.0 -stdlib=libc++" \
"LIBS=-lc++ -lc++abi"

make -j8
make install

make distclean

./configure \
--build=x86_64-apple-$darwin \
--host=armv7-apple-$darwin \
--with-protoc=$protoc \
--disable-shared \
--prefix=$build_dir \
--exec-prefix=$build_dir/arch/armv7 \
"CC=clang" \
"CFLAGS=$cflags -miphoneos-version-min=9.0 -arch armv7 -isysroot $isysroot" \
"CXX=clang" \
"CXXFLAGS=$cxxflags -miphoneos-version-min=9.0 -arch armv7 -isysroot $isysroot" \
LDFLAGS="-arch armv7 -miphoneos-version-min=9.0 -stdlib=libc++" \
"LIBS=-lc++ -lc++abi"

make -j8
make install

lipo \
$build_dir/arch/arm64/lib/libprotobuf-lite.a \
$build_dir/arch/armv7/lib/libprotobuf-lite.a \
-create \
-output $build_dir/lib/libprotobuf-lite.a

lipo \
$build_dir/arch/arm64/lib/libprotobuf.a \
$build_dir/arch/armv7/lib/libprotobuf.a \
-create \
-output $build_dir/lib/libprotobuf.a

rm -rf $build_dir/arch
2、执行 build-ios.sh
1
bash ./build-ios.sh

三、编译android版本

1、先下载 ndk

下载地址:https://developer.android.com/ndk/downloads

本文下载的版本是 r21e

2、生成 独立工具链

r19 或更高版本需要生成

对于 r19 之前的版本,NDK 的默认工具链都是独立工具链

参考:https://developer.android.com/ndk/guides/standalone_toolchain

1
2
3
# $NDK/build/tools/make_standalone_toolchain.py --arch arm --api 21 --install-dir ./tmp/my-android-toolchain

python make_standalone_toolchain.py --arch arm --api 21 --install-dir ../../my-android-toolchain
3、新建buid_script/build-android.sh,并将以下写入后执行 bash ./build-android.sh

参考:https://blog.csdn.net/hjwang1/article/details/44497489

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash

cd ..

NDK=/Users/janwe/DevTools/AdrTools/android-ndk-r21e
TOOLCHAIN=$NDK/my-android-toolchain
export PATH=$PATH:$TOOLCHAIN/bin

SYSROOT=$NDK/my-android-toolchain/sysroot
CXXSTL=$NDK/sources/cxx-stl/llvm-libc++

build_dir=`pwd`/build/android

function build_armv8-a()
{
mkdir -p $build_dir/armv8-a

./configure \
--prefix=$build_dir/armv8-a \
--host=aarch64-linux-android \
--with-sysroot=$SYSROOT \
--enable-static \
--disable-shared \
--enable-cross-compile \
--with-protoc=protoc LIBS="-lc" \
CC="aarch64-linux-android21-clang --sysroot $SYSROOT" \
CFLAGS="-march=armv8-a -D__ANDROID_API__=21" \
CXX="aarch64-linux-android21-clang++ --sysroot $SYSROOT" \
CXXFLAGS="-frtti -fexceptions -march=armv8-a -D__ANDROID_API__=21 -I$CXXSTL/include -I$CXXSTL/libs/arm64-v8a/include -L$CXXSTL/libs/arm64-v8a/ -lc++_static -std=c++11 -stdlib=libc++ -nostdinc++ -fPIC"

make clean
make
make install
}

function build_armv7-a()
{
mkdir -p $build_dir/armv7-a

./configure \
--prefix=$build_dir/armv7-a \
--host=arm-linux-androideabi \
--with-sysroot=$SYSROOT \
--enable-static \
--disable-shared \
--enable-cross-compile \
--with-protoc=protoc LIBS="-lc" \
CC="arm-linux-androideabi-clang --sysroot $SYSROOT" \
CFLAGS="-march=armv7-a" \
CXX="arm-linux-androideabi-clang++ --sysroot $SYSROOT" \
CXXFLAGS="-march=armv7-a -I$CXXSTL/include -I$CXXSTL/libs/armeabi-v7a/include -L$CXXSTL/libs/armeabi-v7a/ -lc++_static -std=c++11 -stdlib=libc++ -nostdinc++ -fPIC"

make clean
make
make install
}

build_armv8-a
build_armv7-a

四、导出 protobuf 的 lua 文件

1、先安装lua
1
2
3
4
curl -R -O http://www.lua.org/ftp/lua-5.1.5.tar.gz
tar zxf lua-5.1.5.tar.gz
cd lua-5.1.5
make all test
2、安装 python 的 Protobuf 库
1
2
3
4
5
6
7
8
9
10
11
# 1、先执行以下 生成相应平台的protoc
make distclean
./configure
make

# 2、进入到protobuf/python文件夹目录中执行
sudo python setup.py install
## error: package directory 'google/protobuf/compiler' does not exist
## 方法:拷贝 src目录下的google/protobuf/compiler到python目录下
# 执行完后可以通过执行以下,检查是否有安装成功
sudo python setup.py test
3、安装 protoc-gen-lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# https://github.com/sean-lin/protoc-gen-lua
git clone https://github.com/sean-lin/protoc-gen-lua

cd protobuf

make
# 出错1 Perhaps you should add the directory containing `lua5.1.pc'
# 方法
cp path/etc/lua.pc /usr/local/lib/pkgconfig/lua5.1.pc

# 出错2
#ifdef _ALLBSD_SOURCE
#include <machine/endian.h>
#else
#include <endian.h>
#endif
/*替换为*/
#include <machine/endian.h>

# 出错3 找不到 protoc-gen-lua
将protobuf同级目录下的plugin plugin_pb2.py protoc-gen-lua 以及生成的pb.so拷贝到/usr/local/bin 目录下就大功告成了

五、编译win32版本

1
2
3
git clone git@github.com:rio789/protobuf-cmake.git

# 编辑 build-2015.bat后,运行脚本

转载请注明来源,欢迎指出任何有错误或不够清晰的表达。可以邮件至 xiyugee@qq.com