问题
I'm trying to cross-compile a gRPC using cmake.
I actually managed to do it. Unfortunately my method involves tinkering inside CMakeLists.txt.
Problem was that, when I was trying to compile gRPC it was using protobuffer he just compiled. It cannot run ARM compiled executables on x86 machine.
I managed to compile it by specifying path to protoc and grpc_cpp_plugin manually in gRPCs main CMakeLists.txt. It is dirty and since I would like to include gRPC as submodule I need clean way to do it.
Has anyone managed to cross-compile gRPC using cmake?
回答1:
This way should work:
Cross-compile Protobuf for target(ARM) and install it.
Make sure that cross-compiled Protobuf can be searched in the toolchain(ARM) you have. E.g. it is installed into system-default prefix under sysroot.
On host, install Protobuf of the same version. It needn't to be searchable (that is, user-local installation is sufficient).
Cross-compile gRPC with following parameters (set in CMake GUI or as
-D
option forcmake
executable):- gRPC_PROTOBUF_PROVIDER: package
- gRPC_PROTOBUF_PACKAGE_TYPE: MODULE
- Protobuf_PROTOC_EXECUTABLE: <path to your host
protoc
executable>
Explanations
Setting parameter gRPC_PROTOBUF_PROVIDER
to "package" tells gRPC to not build its own Protobuf, but use already installed variant. This variant is searched by find_package(Protobuf)
.
Setting parameter gRPC_PROTOBUF_PACKAGE_TYPE
to "MODULE" tells gRPC to not use "Config" file, provided by Protobuf installation, for detect Protobuf things (libraries and executable). Config file contains hardcoded paths which cannot be adjusted outside. Instead, FindProtobuf.cmake script is used for find Protobuf.
Setting parameter Protobuf_PROTOC_EXECUTABLE
tells FindProtobuf.cmake
script to not search Protobuf executable, but takes it from the parameter.
来源:https://stackoverflow.com/questions/52202453/cross-compiling-grpc-using-cmake