gRPC 的库有点多,手工编写 Makefile 不简单,它有如下这么多的库和依赖库:
$ ls
cmake libabsl_log_severity.a libabsl_throw_delegate.a libgrpc.a libgrpc_plugin_support.a libssl.a
libabsl_bad_optional_access.a libabsl_raw_logging_internal.a libaddress_sorting.a libgrpc++.a libgrpcpp_channelz.a libupb.a
libabsl_base.a libabsl_spinlock_wait.a libcares.a libgrpc++_alts.a libgrpc++_reflection.a libz.a
libabsl_dynamic_annotations.a libabsl_strings.a libcrypto.a libgrpc_cronet.a libgrpc_unsecure.a pkgconfig
libabsl_int128.a libabsl_strings_internal.a libgpr.a libgrpc++_error_details.a libgrpc++_unsecure.a
其中,cmake和pkgconfig为目录,如果使用CMake编译则会简单些。以下为一个Makefile示例:
# Protobuf - 必须为 Protobuf3
PROTOBUF_HOME=/usr/local/protobuf
PROTOBUF_INCL=-I${PROTOBUF_HOME}/include
PROTOBUF_LIB=${PROTOBUF_HOME}/lib/libprotobuf.a ${PROTOBUF_HOME}/lib/libprotobuf-lite.a
# gRPC
GRPC_HOME=/usr/local/grpc
GRPC_INCL=-I${GRPC_HOME}/include
ABSL_LIB=${GRPC_HOME}/lib/libabsl_strings.a ${GRPC_HOME}/lib/libabsl_strings_internal.a ${GRPC_HOME}/lib/libabsl_throw_delegate.a ${GRPC_HOME}/lib/libabsl_base.a
GRPC_LIB=${ABSL_LIB} ${GRPC_HOME}/lib/libgpr.a ${GRPC_HOME}/lib/libgrpc++.a ${GRPC_HOME}/lib/libgrpc.a ${GRPC_HOME}/lib/libupb.a ${GRPC_HOME}/lib/libssl.a ${GRPC_HOME}/lib/libcrypto.a ${GRPC_HOME}/lib/libcares.a ${GRPC_HOME}/lib/libaddress_sorting.a
INCLS=${GRPC_INCL} ${PROTOBUF_INCL}
LIBS=-pthread -ldl -lrt -lz -Wl,--start-group ${GRPC_LIB} ${PROTOBUF_LIB} -Wl,--end-group
.PHONY: build
build:
g++ -std=c++11 -Wno-deprecated-declarations -o test test.cpp ../grpc/common.pb.cc ../grpc/proxy.pb.cc ../grpc/proxy.grpc.pb.cc -I../grpc ${INCLS} $(LIBS)
.PHONY: clean
clean:
rm -f test
上述的“-Wno-deprecated-declarations”是为消除下列编译警告:
/usr/local/grpc/include/grpcpp/impl/codegen/proto_utils.h:52:32: 警告:不建议使用‘int google::protobuf::MessageLite::ByteSize() const’(声明于 /usr/local/grpc/include/google/protobuf/message_lite.h:402):Please use ByteSizeLong() instead [-Wdeprecated-declarations]
因为库实在有点多,为避免因为静态库顺序的坑,直接使用了“–start-group”和“–end-group”忽略顺序关系。建议使用CMake或automake编译,以省去处理依赖的麻烦。