gpt4 book ai didi

linker - 静态链接不适用于节点模块

转载 作者:行者123 更新时间:2023-12-04 15:32:12 29 4
gpt4 key购买 nike

我正在为 AWS lambda 开发 native 节点模块。此节点模块需要 json-c

根据 AWS lambda guidelines节点模块不应具有动态依赖关系。所以尝试链接json-c库的静态版本。但我收到编译时错误。

由于节点模块只是一个共享库,我写了一个 sample C application (与 main 重命名)来模拟节点模块编译,这些是结果:

g++      -shared -pthread -rdynamic -m64  -Wl,-soname=addon.node -o addon.node testjson.cpp  -I /usr/include/json-c/  -L  /lib/x86_64-linux-gnu/  -l:libjson-c.a
testjson.cpp: In function ‘int test()’:
testjson.cpp:6:14: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *str = "{ \"msg-type\": [ \"0xdeadbeef\", \"irc log\" ], \
^
/usr/bin/ld: /tmp/ccihB9d8.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccihB9d8.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

当我尝试使用“--whole-archive”时:
g++ -shared -o libshared.so -Wl,--whole-archive -fPIC -l:/usr/lib/x86_64-linux-gnu/libjson-c.a  -Wl,--no-whole-archive testjson.cpp -I /usr/include/json-c/
testjson.cpp: In function ‘int test()’:
testjson.cpp:6:14: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *str = "{ \"msg-type\": [ \"0xdeadbeef\", \"irc log\" ], \
^
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libjson-c.a(json_c_version.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/lib/x86_64-linux-gnu/libjson-c.a(json_c_version.o): error adding symbols: Bad value
collect2: error: ld returned 1 exit status

我在做什么错?不可能将库静态链接到共享对象吗?

最佳答案

首先,您需要手动将 json-c 构建为静态库。

创建 Json-c 静态库

  • wget https://s3.amazonaws.com/json-c_releases/releases/json-c-0.11.tar.gz
  • tar -xvzf json-c-0.11.tar.gz
  • cd json-c-0.11
  • 自动生成器
  • ./configure --enable-static --with-pic --prefix=/var/task。
  • 制作
  • 安装

  • 像这样配置你的 binding.gyp 文件(它适用于使用 node-gyp 工具将源代码构建到 npm 库)。
    {
    "targets": [
    {
    "target_name": "testName",
    "sources": ["yourCode.c"],
    "libraries": ["/var/task/lib/libjson-c.a"]
    }
    ]
    }

    它为我工作。

    关于linker - 静态链接不适用于节点模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37419610/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com