将本机节点模块编译为 Android 时无法 link 库。使用 -fPIC 标志时使用 -fPIC 问题重新编译

Unable to link libraries when compiling native node modules to Android. Recompile with -fPIC issue when using -fPIC flag

这个问题主要与 node-gyp、GCC 和 NDK 工具链有关,但我会添加完整的上下文,因为这可能是必要的。

上下文

我正在使用 React Native 构建应用程序。由于我需要在此应用程序中使用节点本机库,因此我将此节点模块 nodejs-mobile 用于 运行 Node.js 在 Android 和 iOS 上的进程。

添加 composer-admin 模块时,我遇到了 Android 的编译问题。

错误

编译器在链接 composer-admin 依赖项所需的 grpc_node 模块时失败。错误是:

[LIB 1 PATH OMITTED] requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC ... [LIB N PATH OMITTED] requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC clang70++: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [grpc_node.target.mk:189: Release/obj.target/grpc_node.node] Error 1 make: Leaving directory '/home/vanclief/Cacao_repos/react-app/android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project/node_modules/fabric-client/node_modules/grpc/build'

奇怪的是 -fPIC 标志正在按照 common.gypi:

中指定的方式使用

/home/vanclief/Cacao_repos/react-app/android/build/standalone-toolchains/arm-linux-androideabi/bin/arm-linux-androideabi-clang++ -shared -g -rdynamic -fPIC -Wl,-soname=grpc_node.node -o

This is the full compilation log

common.gypi 是错误的。它在 ldflags 中有 -fPIC。它需要在 cflags 中。

您是如何编辑 common.gypi 的? 我有一个非常相似的问题和非常相同的错误 requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC

我看到 common.gypi 有 -fPIC 的 ldflags。我改为关注但仍然遇到同样的问题。 请提供您的修复方法

['OS=="android"', {
        'conditions': [
          [ 'node_shared=="true"', {
            'ldflags': [ '-fPIC' ],

'cflags': [ '-fPIC' ], #Change 1

          }]
        ],
        'target_conditions': [
          ['_toolset=="target"', {
            'defines': [ '_GLIBCXX_USE_C99_MATH' ],
            'libraries': [ '-llog' ],
          }],
          [ '_type=="loadable_module"', {
            'ldflags': [ '-fPIC' ],

'cflags': [ '-fPIC' ], #Change 2

            'conditions': [
              # While loading a native node module, Android needs to have a
              # (NEEDED) entry for libnode.so, or it won't be able to locate
              # referenced symbols.
              # We link to the binary libraries that are distributed with the
              # nodejs-mobile headers so the (NEEDED) entry is created
              [ 'target_arch=="arm"', {
                'libraries': ['>(node_root_dir)/bin/armeabi-v7a/libnode.so'],
              }],
              [ 'target_arch=="arm64"', {
                'libraries': ['>(node_root_dir)/bin/arm64-v8a/libnode.so'],
              }],
              [ 'target_arch=="x86"', {
                'libraries': ['>(node_root_dir)/bin/x86/libnode.so'],
              }],
              [ 'target_arch=="x86_64"', {
                'libraries': ['>(node_root_dir)/bin/x86_64/libnode.so'],
              }],
            ],
          }],
        ],
      }],