grpc need version 3.13 or later of cmake
gcc
https://ftp.gnu.org/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.gz
tar xf gcc-8.3.0.tar.gz
cd gcc-8.3.0
./contrib/download_prerequisites
./configure --prefix=/usr/local/gcc-8.3.0 --enable-bootstrap --enable-languages=c,c++ --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib
make -j$(nproc)
make install
cmake 指定gcc版本
可以直接在shell中对CC和CXX进行指定:
export CC=/usr/local/gcc-8.3.0/bin/gcc
export CXX=/usr/local/gcc-8.3.0/bin/g++
cmake /path/to/your/project
make
也可以直接在cmake的命令行参数中进行指定
cmake -D CMAKE_C_COMPILER=/usr/local/gcc-8.3.0/bin/gcc -D CMAKE_CXX_COMPILER=/usr/local/gcc-8.3.0/bin/g++
因为我们不是替换安装的方式, 所以运行需要的库默认是没有被添加到系统路径下的, 我们还需要额外的一步操作才能让gcc8.3编译出的程序正常的运行:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gcc-8.3.0/lib64
Instal cmake
wget https://github.com/Kitware/CMake/releases/download/v3.20.0/cmake-3.20.0.tar.gz
tar -zxvf cmake-3.20.0.tar.gz
cd cmake-3.20.0
./configure --prefix=/usr/local/cmake
make -j$(nproc)
make install
cmake --version
Download grpc
git clone -b v1.51.0 --depth 1 https://github.com/grpc/grpc
cd grpc
git submodule update --init
Install protobuf
sudo apt-get install autoconf automake libtool curl make g++ unzip
cd ~/grpc/third_party/protobuf
./autogen.sh
./configure
make -j$(nproc)
sudo make install
sudo ldconfig
Install php protobuf extension
cd ~/grpc/third_party/protobuf/php/ext/google/protobuf
phpize
./configure
make && make install
Install grpc
sudo apt-get install build-essential autoconf libtool pkg-config cmake
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gcc-8.3.0/lib64
export CC=/usr/local/gcc-8.3.0/bin/gcc
export CXX=/usr/local/gcc-8.3.0/bin/g++
cd grpc
mkdir -p cmake/build
cd cmake/build
cmake ../.. -DBUILD_SHARED_LIBS=ON -DgRPC_INSTALL=ON
make -j$(nproc)
sudo make install
make clean
sudo ldconfig
```sh
## Install php grpc extension
sudo apt install -y build-essential autoconf libtool pkg-config cd ~/grpc/src/php/ext/grpc phpize ./configure make -j$(nproc) && make install
echo “extension = grpc.so” » /etc/php7/php.ini
## php grpc测试
grpc server
```sh
cd grpc/examples/cpp/helloworld
make -j $(nproc)
./greeter_server
cd grpc/examples/php
composer config repo.packagist composer https://mirrors.aliyun.com/composer/
vim greeter_proto_gen.sh
PROTOC=/usr/local/bin/protoc
PLUGIN=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin
./greeter_proto_gen.sh
composer install
./run_greeter_client.sh
# 已安装grpc扩展但报错Root composer.json requires grpc/grpc
# 解决:添加阿里云
composer config repo.packagist composer https://mirrors.aliyun.com/composer/
Error
grpc/src/php/ext/grpc configure: error: wrong gpr lib version or lib not found 导入新版gcc库,重新编译grpc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gcc-8.3.0/lib64
cd ~/grpc/cmake/build
cmake ../.. -DBUILD_SHARED_LIBS=ON -DgRPC_INSTALL=ON
make -j$(nproc) && make install
make 报错 fatal error: third_party/utf8_range/utf8_range.h: No such file or directory
pecl install protobuf
chmod +x `php-config --extension-dir`/protobuf.so
或者编译安装
wget https://github.com/protocolbuffers/protobuf/releases/download/v21.10/protobuf-php-3.21.10.tar.gz