目錄html
一、下載源碼linux
git clone https://github.com/grpc/grpc.git
二、下載依賴項,grpc的依賴項都以子模塊的方式記錄在third_party
目錄下,因此直接同步子模塊便可。c++
git submodule update --init # 可使用 --recursive 選項遞歸子模塊
文件數比較多,完整下載後打包大小有804MB(PS.都是完整克隆)。git
三、安裝gogithub
若是不按照go,則在生成編譯腳本的時候會報如下錯誤:golang
CMake Error at third_party/boringssl/CMakeLists.txt:38 (message): Could not find Go
Windows上直接下載安裝包 go1.12.5.windows-amd64 安裝便可。express
Linux下也能夠直接下載安裝包安裝,或者是使用相應發行版的包管理器安裝。windows
四、安裝perlcentos
編譯過程很簡單,就是編譯的東西比較多,速度比較慢。下面記錄下編譯的具體過程。bash
在生成VS2015解決方案前須要先安裝cmake 3.x
工具,已經VS開發環境。我這裏是使用VS2019進行的編譯,但也適用於VS2015/VS2017,這是執行cmake
命令行工具的時候指定的參數稍有不一樣。
指定編譯平臺工具集和編譯配置類型等
# 下面-G/-A 選項僅適用於VS2019,若是是VS2017或2015等,則須要使用-G "Visual Studio 15 2017 Win64"形式 —G "Visual Studio 16 2019" -A x64 \ -DCMAKE_CONFIGURATION_TYPES=Release # 或者 DCMAKE_BUILD_TYPE=Release
指定go.exe路徑
-DGO_EXECUTABLE="C:/Go/bin/go.exe"
指定安裝輸出路徑
-DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" \ # 下面幾個也能夠不指定 -DINSTALL_BIN_DIR="E:/complier/grpc/install/grpc/bin" \ -DINSTALL_MAN_DIR="E:/complier/grpc/install/grpc/share/man" \ -DINSTALL_INC_DIR="E:/complier/grpc/install/grpc/include" \ -DINSTALL_PKGCONFIG_DIR="E:/complier/grpc/install/grpc/share/pkgconfig" \
指定使用靜態運行時庫
-DgRPC_MSVC_STATIC_RUNTIME=1
這裏和下面編譯第三方庫時候都編譯使用靜態(MT)運行時的是對應的,若是下面編譯第三方庫都指定動態運行時庫(MD),則無需指定該選項。
ZLIB
警告。意思就是由於gRPC_ZLIB_PROVIDER
是module
,因此強制設置gRPC_INSTALL
爲FALSE
。
CMake Warning at cmake/zlib.cmake:32 (message): gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is "module" Call Stack (most recent call first): CMakeLists.txt:140 (include)
解決這個警告須要使用到zlib庫,關於它的編譯這裏就再也不說了,能夠查看 VS編譯 x64版本zlib庫 的內容。
我這裏就沒有去編譯它,直接使用了之前vcpkg安裝的。而後指定以下選項繼續。
-DgRPC_ZLIB_PROVIDER=package \ -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" \ -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" \ -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib"
CARES
警告。這個警告能夠不用管,也能正常編譯經過。CMake Warning at cmake/cares.cmake:33 (message): gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is "module" Call Stack (most recent call first): CMakeLists.txt:141 (include)
但我這裏仍是解決它,先下載 c-ares 編譯安裝。
# 下載源碼(在grpc\third_party\cares\cares目錄下也是同樣的,能夠不用下載) git clone https://github.com/c-ares/c-ares.git # 生成VS工程 cd c-ares # 注意,下面必須指定CARES_MSVC_STATIC_RUNTIME選項,不然後面編譯grpc時候會通不過(找不到__imp__xxx) cmake . -G "Visual Studio 16 2019" -A x64 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static -DCARES_STATIC=1 -DCARES_SHARED=0 -DCARES_MSVC_STATIC_RUNTIME=1 # 編譯 msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 c-ares.sln # 安裝,上面生成VS工程時候指定了安裝在當E:/complier/x64-windows-static目錄下 msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 INSTALL.vcxproj
而後指定下面幾個選項後繼續。
-DgRPC_CARES_PROVIDER=package \ -Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" # 這裏是指定到c-ares-config.cmake文件的路徑
PROTOBUF
警告-- 3.8.0.0 CMake Warning (dev) at third_party/protobuf/cmake/install.cmake:60 (message): The file "E:/complier/grpc/grpc/third_party/protobuf/src/google/protobuf/stubs/io_win32.h" is listed in "E:/complier/grpc/grpc/third_party/protobuf/cmake/cmake/extract_includes.bat.in" but there not exists. The file will not be installed. Call Stack (most recent call first): third_party/protobuf/cmake/CMakeLists.txt:231 (include) This warning is for project developers. Use -Wno-dev to suppress it. CMake Warning at cmake/protobuf.cmake:51 (message): gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is "module" Call Stack (most recent call first): CMakeLists.txt:142 (include)
解決這個問題的步驟以下:
一、進入grpc/third_party/protobuf
目錄,使用git
命令更新下子模塊git submodule update --init
。或者在一開始對grpc
更新子模塊的時候,使用--recursive
進行遞歸更新。
二、生成編譯配置,編譯安裝(參考:protocolbuffers/protobuf/appveyor.bat)
# 參考protobuf目錄下的 appveyor.bat 腳本文件 mkdir build_msvc cd build_msvc # 指定 protobuf_BUILD_SHARED_LIBS 爲0,以便生成靜態庫 cmake -G "Visual Studio 14 2015 Win64" -Dprotobuf_BUILD_SHARED_LIBS=0 -Dprotobuf_UNICODE=1 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static ../cmake # 編譯,這裏指定生成了Release版本的,默認是生成的Debug版本(實際上我兩個版本都編譯了) msbuild ALL_BUILD.vcxproj /maxcpucount:4 /p:Configuration=Release # 安裝 msbuild INSTALL.vcxproj /p:Configuration=Release
三、指定下面幾個選項後,繼續使用cmake命令生成
-DgRPC_CARES_PROVIDER="package" \ -DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include" \ -DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" \ -DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" \ -DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" \ -DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" \ -DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" \ -DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" \ -DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" \ -DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" \
SSL
警告CMake Warning at cmake/ssl.cmake:37 (message): gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is "module" Call Stack (most recent call first): CMakeLists.txt:143 (include)
這裏能夠忽略它,不使用SSL便可。也能夠編譯openssl庫,而後指定如下選項後,繼續執行cmake。
-DgRPC_SSL_PROVIDER="package" \ -DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" \ -DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" \ -DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" \ -DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" \ -DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" \ -DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" \ -DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" \ -DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" \ -DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" \
編譯openssl也很簡單,能夠參考 VS2015編譯Openssl-1.1.0f 的內容,也能夠直接使用vcpkg
安裝。
我這裏沒有編譯了,直接使用vcpkg安裝後,備份出來後使用。
vcpkg install openssl:x64-windows-static vcpkg export openssl:x64-windows-static --zip --output=openssl.x64-windows-static
下面兩個錯誤,是我在指定編譯器爲VS2015的時候遇到的,VS2019並無遇到。
一、繼續,報錯誤INTTYPES_FORMAT
沒有設置
CMake Error at third_party/gflags/CMakeLists.txt:286 (message): Do not know how to define a 32-bit integer quantity on your system! Neither uint32_t, u_int32_t, nor __int32 seem to be available. Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.
這裏能夠打開grpc/third_party/gflags/CMakeLists.txt 查看
這裏在cmake命令參數中添加一個變量,指定使用C99或者VC7格式。
-DINTTYPES_FORMAT:STRING=C99
二、繼續,報benchmark
相關錯誤
-- git Version: v1.4.0-e776aa02-dirty -- Version: 1.4.0 -- Performing Test HAVE_STD_REGEX -- failed to compile -- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile -- Performing Test HAVE_POSIX_REGEX -- failed to compile CMake Error at third_party/benchmark/CMakeLists.txt:229 (message): Failed to determine the source files for the regular expression backend
我直接打開grpc/third_party/benchmark/CMakeLists.txt作了以下修改,而後繼續執行cmake,就經過了
cmake —G "Visual Studio 16 2019" -DCMAKE_CONFIGURATION_TYPES=Release -DGO_EXECUTABLE="C:/Go/bin/go.exe" -DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" -DgRPC_MSVC_STATIC_RUNTIME=1 -DgRPC_ZLIB_PROVIDER=package -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib" -DgRPC_CARES_PROVIDER=package -Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" -DgRPC_PROTOBUF_PROVIDER="package" -DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" -DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" -DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" -DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" -DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" -DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" -DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" -DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" -DgRPC_SSL_PROVIDER="package" -DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DINTTYPES_FORMAT:STRING=C99 ..\grpc
直接使用VS2019命令行工具進入生成的工程目錄,執行下面命令便可
msbuild /maxcpucount:4 /p:Configuration=Release ALL_BUILD.vcxproj
編譯耗時會比較久。
注意,上面編譯第三方庫的時候,必定要指定使用使用靜態運行時(/MT),不然可能這裏編譯完連接會出錯。或者能夠乾脆不編譯靜態庫,所有編譯爲動態庫,使用默認的(/MD)參數。
這裏只作了CentOS7下的編譯,若是是其餘發行版應該更簡單。
uname -a Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
下面不少操做須要root權限,下面的命令沒有使用sudo
,須要根據狀況添加。
這裏主要是安裝如下gcc編譯器和cmake等工具。
# 安裝cmake三、make(默認cmake版本過低) yum -y install epel-release # 默認狀況下是沒有cmake3的,必須安裝epel(企業版 Linux 附加軟件包)源才行 yum install cmake3 make # 配置SLC源 yum install centos-release-scl-rh centos-release-scl yum check-update # 安裝devtoolset,由於默認安裝gcc版本爲4.8.5,版本過低 yum install devtoolset-8-gcc devtoolset-8-gcc-c++ # 安裝完成以後執行下面語句,把相關環境變量導入 source /opt/rh/devtoolset-4/enable # 也能夠把上面文件的內容追加到/etc/profile中,就不用每次都要導入了 # cat /opt/rh/devtoolset-4/enable >> /etc/profile # 安裝go,也可直接下載安裝包 yum install go-toolset-7-golang # 啓用go,這裏和上面同樣,也能夠直接寫到/etc/profile裏面去 source /opt/rh/go-toolset-7/enable
一、編譯安裝zlib
cd grpc/third_party/zlib # 編譯安裝 ./configure --prefix=/usr make && make install
二、編譯安裝c-ares
git clone https://github.com/c-ares/c-ares.git cd c-ares # 能夠不下載,直接 cd grpc/third_party/cares/cares cmake3 . -DCMAKE_INSTALL_PREFIX=/usr make && make install
三、編譯安裝protobuf
# 進入grpc下源碼目錄,或者下載 git clone https://github.com/protocolbuffers/protobuf.git cd grpc/third_party/protobuf # 同步下子模塊 git submodule update --init --recursive # 建立cmake構建目錄 mkdir build && cd build # 生成Makefile cmake3 -DCMAKE_INSTALL_PREFIX=/usr ../cmake # 編譯安裝 make -j4 make install
四、編譯安裝openssl
這個也能夠不安裝的,不影響編譯。
# 下載安裝包 wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz tar -xzf openssl-1.0.2s.tar.gz cd openssl-1.0.2s # 配置編譯參數 ./config --prefix=/usr # 編譯安裝 make -j4 make install
注意,由於我上面編譯的幾個庫都是安裝到/usr
目錄的,因此下面cmake參數中沒有再指定。若是是安裝到自定義的目錄的,則須要像上面Windows下編譯同樣,指定相關的路徑。
這裏沒什麼直接使用
# 建立cmake構建目錄 mkdir build && cd build # 執行cmake命令,生成Makefile腳本 cmake3 ../grpc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/pure/grpc/install -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_SSL_PROVIDER=package # 編譯 make -j4