一. 編譯環(huán)境說明
1. grpc版本: V1.58.0
2. gcc版本:gcc7或以上(當(dāng)前使用 gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04))
3. cmake版本:3.8或以上(當(dāng)前使用cmake version 3.19.6)
4. 操作系統(tǒng)版本:Ubuntu16.04
5. apt 源:阿里云
二. 編譯環(huán)境準(zhǔn)備
說明:由于Ubuntu16.04自帶編譯環(huán)境,gcc為4.8,cmake為3.5,不滿足編譯grpc-v1.58.0最低要求, 因此需要升級(jí)gcc和cmake環(huán)境。
1. 更新gcc
$ apt-get install build-essential autoconf libtool pkg-config
2. 更新cmake
# 1.下載自動(dòng)安裝腳本
$ wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.19.6/cmake-3.19.6-Linux-x86_64.sh
# 2.執(zhí)行自動(dòng)安裝腳本(--prefix指定cmake安裝目錄)
$ sh cmake-linux.sh -- --skip-license --prefix=/usr/local/cmake
# 3.刪除自動(dòng)安裝腳本
$ rm cmake-linux.sh
# 4.將cmake安裝目錄永久設(shè)置到PATH環(huán)境變量
$ vim /etc/profile
(最后一行新增:export PATH=/usr/local/cmake/bin:$PATH)
$ source /etc/profile
三. 源碼下載
# 注意:下載子模塊時(shí)因github對(duì)應(yīng)源碼不在國內(nèi)服務(wù)器,下載會(huì)很慢,最好使用梯子
git clone --recurse-submodules -b v1.58.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
四、源碼編譯安裝gRPC和Protocol Buffers(命令行)
# 1.進(jìn)入grpc源碼根路徑
$ cd grpc
# 2.創(chuàng)建編譯目錄
$ mkdir -p cmake/build
# 3.進(jìn)入編譯目錄
$ pushd cmake/build
# 4.執(zhí)行cmake設(shè)置cmake參數(shù),并指定安裝目錄為/usr/local/grpc(該命令會(huì)轉(zhuǎn)換成makefile供編譯使用)
$ cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/grpc ../..
# 5.編譯
$ make -j 4
# 6.安裝(將編譯后的目標(biāo)文件安裝到/usr/local/grpc)
$ make install
# 7.退出編譯目錄
$ popd
###################################################################
# 至此,grpc源碼庫已編譯完成,生成的相關(guān)頭文件、庫文件在/usr/local/grpc目錄下
# 8. 查看編譯后的目標(biāo)文件
$ ls /usr/local/grpc
bin include lib share
四、源碼編譯安裝gRPC和Protocol Buffers(Qt Creator)
1. 設(shè)置Qt Creator CMake環(huán)境為3.19.0,并設(shè)置為默認(rèn)套件
文章來源:http://www.zghlxwxcb.cn/news/detail-859972.html
- 修改構(gòu)建套件的CMake Tool為新建的CMake
2. 打開grpc項(xiàng)目
-
- 選擇grpc源碼目錄中的CMakeLists.txt(grpc/CMakeLists.txt)
-
- 初始打開,會(huì)彈出窗,設(shè)置cmake編譯的構(gòu)建目錄;(構(gòu)建產(chǎn)生的構(gòu)建文件都會(huì)生成在該目錄下)
-
- 初始打開,設(shè)置構(gòu)建目錄后,會(huì)彈出窗設(shè)置CMake參數(shù);
-
- 點(diǎn)擊CMake,執(zhí)行CMake;(最終會(huì)生成MakeFile文件)
- 3)、4)步驟,等同于命令 cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/grpc …/…;該命令設(shè)置參數(shù)并執(zhí)行cmake。
-
- 然后項(xiàng)目打開完成。打開后工程結(jié)構(gòu)如下:
- 然后項(xiàng)目打開完成。打開后工程結(jié)構(gòu)如下:
3. 修改構(gòu)建設(shè)置,添加構(gòu)建步驟
-
- 默認(rèn)構(gòu)建設(shè)置中,構(gòu)建步驟只有make命令,如果希望執(zhí)行make install,可以修改構(gòu)建設(shè)置,添加make install 構(gòu)建步驟
- 默認(rèn)構(gòu)建設(shè)置中,構(gòu)建步驟只有make命令,如果希望執(zhí)行make install,可以修改構(gòu)建設(shè)置,添加make install 構(gòu)建步驟
4. 構(gòu)建(編譯項(xiàng)目)
文章來源地址http://www.zghlxwxcb.cn/news/detail-859972.html
五、樣例程序編譯(使用已有CMakeLists.txt,全部編譯)
$ cd examples/cpp/helloworld
$ mkdir -p cmake/build
$ pushd cmake/build
# 執(zhí)行cmake,生成makefile文件(CMAKE_PREFIX_PATH 指定CMake的搜索路徑,提供給find_package(), find_program(), find_library(), find_file(), 和find_path()等函數(shù)使用。
# 這里還有一個(gè)小知識(shí)點(diǎn)就是,CMAKE_INSTALL_PREFIX (執(zhí)行make install命令時(shí)安裝的根目錄的路徑) 會(huì)被添加到CMAKE_SYSTEM_PREFIX_PATH,所以find_package(), find_program(), find_library(), find_path(), find_ile()等命令也可以該目錄為prefix去查找。)
$ cmake -DCMAKE_PREFIX_PATH=/usr/local/grpc ../..
#編譯(如果多線程編譯報(bào)錯(cuò),則直接使用make進(jìn)行編譯即可)
$ make -j 4
############################
# 編譯后,會(huì)生成如下文件:
root@node2:~/software/grpc/examples/cpp/helloworld/cmake/build# ls
CMakeCache.txt cmake_install.cmake greeter_async_client2 greeter_callback_client greeter_client helloworld.grpc.pb.cc helloworld.pb.cc libhw_grpc_proto.a
CMakeFiles greeter_async_client greeter_async_server greeter_callback_server greeter_server helloworld.grpc.pb.h helloworld.pb.h Makefile
六、樣例程序編譯(單獨(dú)編譯)
# 1.進(jìn)入到樣例程序目錄
$ cd /root/software/grpc/examples/cpp/helloworld
# 2.使用 protoc 和 gRPC C++ 插件從 helloworld.proto 文件生成 gRPC 服務(wù)端和客戶端接口,生成的代碼將被放置在當(dāng)前目錄中(該命令會(huì)生成helloworld.grpc.pb.h和helloworld.grpc.pb.cc兩個(gè)文件)
$ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/helloworld.proto
# 3.使用protoc和C++插件從helloworld.proto文件生成相應(yīng)的數(shù)據(jù)接口,即響應(yīng)和請求消息類(該命令會(huì)生成helloworld.pb.h和helloworld.pb.cc兩個(gè)文件)
$ protoc -I ../../protos --cpp_out=. ../../protos/helloworld.proto
# 4.設(shè)置PKG_CONFIG_PATH變量(grpc相關(guān)庫通過pkg-config管理,修改該變量指定pkg-config到grpc的安裝目錄里搜索pkgconfig)
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/grpc/lib/pkgconfig
##### re2庫在grpc目錄中沒有re2.pc文件,需要單獨(dú)指定搜索路徑;否則使用pkgconfig會(huì)有如下錯(cuò)誤
<<ErrorMsg
Package re2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `re2.pc'
to the PKG_CONFIG_PATH environment variable
Package 're2', required by 'grpc', not found
In file included from helloworld.pb.cc:4:0:
ErrorMsg
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/root/software/grpc/third_party/re2/
# 5.定義變量管理protobuf相關(guān)公共依賴庫(目的使得g++編譯命令簡短一點(diǎn))
$ export PROTOBUF_ABSL_DEPS=absl_absl_check absl_absl_log absl_algorithm absl_base absl_bind_front absl_bits absl_btree absl_cleanup absl_cord absl_core_headers absl_debugging absl_die_if_null absl_dynamic_annotations absl_flags absl_flat_hash_map absl_flat_hash_set absl_function_ref absl_hash absl_layout absl_log_initialize absl_log_severity absl_memory absl_node_hash_map absl_node_hash_set absl_optional absl_span absl_status absl_statusor absl_strings absl_synchronization absl_time absl_type_traits absl_utility absl_variant
# 6.使用greeter_server.cc編譯服務(wù)端程序
$ g++ -o greeter_server helloworld.pb.cc helloworld.grpc.pb.cc greeter_server.cc -I/usr/local/grpc/include -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ absl_flags absl_flags_parse $PROTOBUF_ABSL_DEPS` -lutf8_validity -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl
# 7.使用greeter_client.cc編譯客戶端程序
$ g++ -o greeter_client helloworld.pb.cc helloworld.grpc.pb.cc greeter_client.cc -I/usr/local/grpc/include -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ absl_flags absl_flags_parse $PROTOBUF_ABSL_DEPS` -lutf8_validity -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl
七、Protobuf編譯器protoc命令常用參數(shù)項(xiàng)
- -I …/…/protos:告訴 protoc 在何處查找 .proto 文件。在這種情況下,它將在 …/…/protos 目錄中查找
- –grpc_out=.:指定生成的 gRPC 服務(wù)文件的輸出目錄。在這種情況下,它們將被生成在當(dāng)前目錄中。
- –plugin=protoc-gen-grpc=which grpc_cpp_plugin``:告訴 protoc 使用哪個(gè)插件來生成 gRPC 代碼。在這種情況下,它使用的是 grpc_cpp_plugin,它是 gRPC C++ 插件。
- –cpp_out=.:指定生成的C++代碼文件的輸出目錄。在這種情況下,它們將被生成在當(dāng)前目錄中。
到了這里,關(guān)于【安裝】grpc源碼編譯安裝(Linux)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!