cmake 添加--coverage标志时未定义grpc::experimental的vtable引用

pod7payv  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(247)

我有一个基于CMake的C++项目。该项目的起始点是使用cmake-init创建的。我试图将gRPC添加到该项目中。但是,当使用覆盖率标志时,发生链接器错误:

build] /usr/bin/ld: CMakeFiles/demo_lib.dir/source/lib.cpp.o: warning: relocation against `_ZTVN4grpc12experimental38FileWatcherAuthorizationPolicyProviderE' in read-only section `.text._ZN4grpc12experimental38FileWatcherAuthorizationPolicyProviderC2EP34grpc_authorization_policy_provider[_ZN4grpc12experimental38FileWatcherAuthorizationPolicyProviderC5EP34grpc_authorization_policy_provider]'
[build] /usr/bin/ld: /usr/bin/ld: ../CMakeFiles/demo_lib.dir/source/lib.cpp.o: warning: relocation against `_ZTVN4grpc12experimental38FileWatcherAuthorizationPolicyProviderE' in read-only section `.text._ZN4grpc12experimental38FileWatcherAuthorizationPolicyProviderC2EP34grpc_authorization_policy_provider[_ZN4grpc12experimental38FileWatcherAuthorizationPolicyProviderC5EP34grpc_authorization_policy_provider]'
[build] /usr/bin/ld: ../CMakeFiles/demo_lib.dir/source/lib.cpp.o: in function `grpc::experimental::StaticDataAuthorizationPolicyProvider::StaticDataAuthorizationPolicyProvider(grpc_authorization_policy_provider*)':
[build] /home/toto/development/cmake-init-tuto/demo/build/coverage/vcpkg_installed/x64-linux/include/grpcpp/security/authorization_policy_provider.h:48: undefined reference to `vtable for grpc::experimental::StaticDataAuthorizationPolicyProvider'
[build] /usr/bin/ld: ../CMakeFiles/demo_lib.dir/source/lib.cpp.o: in function `grpc::experimental::FileWatcherAuthorizationPolicyProvider::FileWatcherAuthorizationPolicyProvider(grpc_authorization_policy_provider*)':
[build] /home/toto/development/cmake-init-tuto/demo/build/coverage/vcpkg_installed/x64-linux/include/grpcpp/security/authorization_policy_provider.h:73: undefined reference to `vtable for grpc::experimental::FileWatcherAuthorizationPolicyProvider'
[build] CMakeFiles/demo_lib.dir/source/lib.cpp.o: in function `grpc::experimental::StaticDataAuthorizationPolicyProvider::StaticDataAuthorizationPolicyProvider(grpc_authorization_policy_provider*)':
[build] /home/toto/development/cmake-init-tuto/demo/build/coverage/vcpkg_installed/x64-linux/include/grpcpp/security/authorization_policy_provider.h:48: undefined reference to `vtable for grpc::experimental::StaticDataAuthorizationPolicyProvider'
[build] /usr/bin/ld: CMakeFiles/demo_lib.dir/source/lib.cpp.o: in function `grpc::experimental::FileWatcherAuthorizationPolicyProvider::FileWatcherAuthorizationPolicyProvider(grpc_authorization_policy_provider*)':
[build] /home/toto/development/cmake-init-tuto/demo/build/coverage/vcpkg_installed/x64-linux/include/grpcpp/security/authorization_policy_provider.h:73: undefined reference to `vtable for grpc::experimental::FileWatcherAuthorizationPolicyProvider'

与这里类似问题的不同之处在于,如果没有coverage标志,就不会发生这种情况+我不以任何方式使用AuthorizationPolicy。
以下是CMakePresets.json中的相关标志:

{
      "name": "flags-unix",
      "hidden": true,
      "cacheVariables": {
        "CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wformat=2 -Wundef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wimplicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wold-style-cast -Wl,--whole-archive -Wl,--allow-multiple-definition"
      }
    },
{
      "name": "coverage-unix",
      "binaryDir": "${sourceDir}/build/coverage",
      "inherits": "ci-unix",
      "hidden": true,
      "cacheVariables": {
        "ENABLE_COVERAGE": "ON",
        "CMAKE_BUILD_TYPE": "Coverage",
        "CMAKE_CXX_FLAGS_COVERAGE": "-Og -g --coverage -fkeep-inline-functions -fkeep-static-functions",
        "CMAKE_EXE_LINKER_FLAGS_COVERAGE": "--coverage",
        "CMAKE_SHARED_LINKER_FLAGS_COVERAGE": "--coverage",
        "CMAKE_MAP_IMPORTED_CONFIG_COVERAGE": "Coverage;RelWithDebInfo;Release;Debug;"
      }
    },

编辑:
引用的标题如下所示:https://grpc.github.io/grpc/cpp/authorization__policy__provider_8h_source.html(只有80行)。在我看来,没有任何纯虚方法没有被定义。我错了吗?
下面是源文件的外观:https://github.com/faaxm/exmpl-cmake-grpc/blob/master/server/src/main.cpp,只是在我的例子中,它是一个void launch_server()函数而不是int main(),并且实际的int main()调用launch_server()。我得到的一个(可能是)提示是ServerBuilder类('grpccpp/server_builder.h')具有:

class ServerBuilder {
 public:
  ServerBuilder();
  virtual ~ServerBuilder();
  // (...)
  /// NOTE: class experimental_type is not part of the public API of this class.
  /// TODO(yashykt): Integrate into public API when this is no longer
  /// experimental.
  class experimental_type {
   public:
    // (...)

    /// Sets server authorization policy provider in
    /// GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER channel argument.
    void SetAuthorizationPolicyProvider(
        std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
            provider);

   private:
    ServerBuilder* builder_;
  };
  //(...)
  private:
  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
      authorization_provider_;

这是AuthorizationPolicyProvider以某种方式进入我的代码的唯一方法,通过gRPC ServerBuilder类。

cxfofazt

cxfofazt1#

IIRC这与一个叫做位置无关代码的东西有关。你实际上有一个静态库,你想链接到一个共享库中(这很可能是由--coverage标志引起的),没有位置无关代码你就不能做到这一点。你需要做的是通过传递-fPIC重新编译静态库。
然后你可以重新编译你的项目,它应该工作。
编辑:**Here is a link**到堆栈溢出问题,并解释如何判断库是否使用-fPIC编译。

相关问题