gRPC 不为 ubuntu 上的 c++ 生成库

gRPC doesn't produce libraries for c++ on ubuntu

本文关键字:c++ 上的 不为 ubuntu gRPC      更新时间:2023-10-16

当我试图在文件夹gRPC/examples/cpp/helloworld中运行gRPC for c++的示例时,它需要使用Cmake 构建gRPC时未编译的库

首先,我在Ububtu 16.04中使用说明构建了gRPC:

$ git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
$ cd grpc
$ git submodule update --init
$ cd third_party/protobuf
$ git submodule update --init --recursive
$ ./autogen.sh
$ ./configure --prefix=/usr
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.
$ pkg-config --cflags protobuf         # print compiler flags
$ pkg-config --libs protobuf           # print linker flags
$ pkg-config --cflags --libs protobuf  # print both
cd ../..
make
sudo make install 

之后,我尝试在grpc/examples/cpp/helloworld 文件夹中运行示例

grps/grpc/examples/cpp/helloworld$ make

我犯了几个错误,通过将grpcpppplugin从文件夹grpc/bins/opt复制到/usr/local/bin,并将grpc++.pc和grpc++_unsecure.pc从grpc/libs/opt/pkgconfig/复制到/usr/local/lib/pkgconfig,我解决了这些错误。当我尝试下一次命令时

grpc/examples/cpp/helloworld$ make

我收到信息

g++ helloworld.pb.o helloworld.grpc.pb.o greeter_client.o -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_client
/usr/bin/ld: cannot find -lgrpc++
/usr/bin/ld: cannot find -lgrpc++_reflection
collect2: error: ld returned 1 exit status
Makefile:44: recipe for target 'greeter_client' failed
make: *** [greeter_client] Error 1

所以,我在grpc/libs/opt文件夹中搜索了这些libs-libgrpc++,但只有这些库

grpc/libs/opt$ ls --l 
libaddress_sorting.a         libgrpc_cronet.so.8
libaddress_sorting.so        libgrpc_cronet.so.8.0.0
libaddress_sorting.so.8      libgrpc_plugin_support.a
libaddress_sorting.so.8.0.0  libgrpc.so
libares.a                    libgrpc.so.8
libboringssl.a               libgrpc.so.8.0.0
libgpr.a                     libgrpc_unsecure.a
libgpr.so                    libgrpc_unsecure.so
libgpr.so.8                  libgrpc_unsecure.so.8
libgpr.so.8.0.0              libgrpc_unsecure.so.8.0.0
libgrpc.a                    pkgconfig
libgrpc_cronet.a             protobuf
libgrpc_cronet.so

所以make没有为gRPC编译静态和动态库。是我做错了什么,还是没有做错什么,或者有漏洞?protobuf的版本是

:~$ protoc --version
libprotoc 3.8.0
:~$ which protoc
/usr/bin/protoc

以下是我从根目录运行"make"后的一些输出

[MAKE]    Generating /home/user/cpp_test/grps/grpc/libs/opt/pkgconfig/grpc++.pc
[MAKE]    Generating /home/user/cpp_test/grps/grpc/libs/opt/pkgconfig/grpc++_unsecure.pc

因此,它为"libgrpc++*"库创建pkgconfig文件,但不创建这些库
这些具有libgrpc++

libgrpc++ depbase=`echo google/protobuf/io/tokenizer.lo | sed 's|[^/]*$|.deps/&|;s|.lo$||'`;

libgrpc++ depbase=`echo google/protobuf/util/delimited_message_util.lo | sed 's|[^/]*$|.deps/&|;s|.lo$||'`;

只有两条线

看起来您只从third_party/protobuf目录运行了make(第一步需要这样做),并从helloworld目录运行了make。如果您还没有这样做,您应该根据文档从grpc存储库root目录运行make。这将确保libgrpc++*C++库的构建。

所以,我解决了这个问题。当我在根gRPC文件夹上运行"make"时,编译以这样的结果结束:

[CXX]     Compiling /home/user/cpp_test/grps/grpc/gens/src/proto/grpc/core/stats.pb.cc
/home/user/cpp_test/grps/grpc/gens/src/proto/grpc/core/stats.pb.cc:187:13: error: ‘dynamic_init_dummy_src_2fproto_2fgrpc_2fcore_2fstats_2eproto’ defined but not used [-Werror=unused-variable]
static bool dynamic_init_dummy_src_2fproto_2fgrpc_2fcore_2fstats_2eproto = []()
^
cc1plus: all warnings being treated as errors
Makefile:2924: recipe for target '/home/user/cpp_test/grps/grpc/objs/opt//home/user/cpp_test/august/grpc/gens/src/proto/grpc/core/stats.pb.o' failed
make: *** [/home/user/cpp_test/grps/grpc/objs/opt//home/user/cpp_test/august/grpc/gens/src/proto/grpc/core/stats.pb.o] Error 1 

因为所有警告都被视为错误。另一个库的编译也停止了。因此,我在根目录gRPC的Makefile中手动添加了357行末尾的标志-Wno-unused-variable。添加此标志后,gRPC库的构建成功,并构建了所有的libgrpc++*和libgrpc*库。

CPPFLAGS += -g -Wall -Wextra -Werror $(W_NO_UNKNOWN_WARNING_OPTION) -Wno-long-long -Wno-unused-parameter -Wno-deprecated-declarations -Wno-sign-conversion -Wno-shadow -Wno-conversion -Wno-implicit-fallthrough -Wno-sign-compare -Wno-missing-field-initializers -Wno-maybe-uninitialized -DPB_FIELD_32BIT -DOSATOMIC_USE_INLINED=1 -Ithird_party/nanopb -Ithird_party/upb -Isrc/core/ext/upb-generated -Wno-unused-variable