Search code examples
c++cmakelinkergrpccode-coverage

Undefined reference to vtable for grpc::experimental when adding --coverage flag


I have a C++ project based on CMake. The starting point of the project was created using cmake-init. I am trying to add gRPC to the project. However, when using coverage flags, a linker error occurs:

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'

The thing that differs from similar questions here is that without the coverage flags, this does not happen + I do not use AuthorizationPolicy in any way. The code I am using right now is essentially what you can find in https://github.com/faaxm/exmpl-cmake-grpc

Here is the relevant flags from the 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;"
      }
    },

Edit:

The header that is referenced is here: https://grpc.github.io/grpc/cpp/authorization__policy__provider_8h_source.html (it's only 80 lines). it does not seem to me that any pure-virtual method are not defined. Am I wrong?

This is what the source file look like: https://github.com/faaxm/exmpl-cmake-grpc/blob/master/server/src/main.cpp except that in my case, it's a void launch_server() function instead of int main(), and the actual int main() calls launch_server(). One (maybe) hint that I have is that the ServerBuilder class ('grpcpp/server_builder.h') has:

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_;

This is the only way that the AuthorizationPolicyProvider somehow gets into my code, through the gRPC ServerBuilder class. What should I do then?


Solution

  • IIRC this is related to something called Position Independent Code. You essentially have a static library that you wish to link in a shared library (this is most likely caused by the --coverage flag), you can't do that without position independent code. What you need to do is recompile the static libraries by passing -fPIC.

    Then you can recompile your project and it should work.

    EDIT: Here is a link to a stackoverflow question with an explanation on how to figure out if the library was compiled with -fPIC.