NodeJS:node-gyp 使用等效的 gcc -lm 选项进行编译

NodeJS: node-gyp compile with the equivalent gcc -lm option

我是用 C++ 开发 nodejs 插件的新手。

我有一个包含在我的插件中的静态 c 库,需要该库才能与自定义硬件通信,但该库的某些功能使用 math.h。因此,当我在 C 示例中编译时,我这样做:gcc main.c libmackrnp6_0_2_fPIC.a -o test -lm,没问题,但是当我将此库包含在 cpp 插件中时,我的 pow 函数出现问题。

如何使用 -lm 选项(相当于 gcc)编译插件 sudo node-gyp configure build

binding.gyp

{
   "targets": [
       {
           "target_name": "octotuner",

           "sources": [
                "params.cpp",
                "headerv6.h"
            ],
            "libraries": ["/home/nvidia/webrf/api/libmackrnp6_0_2_fPIC.a"],
            "link_settings": {
               "libraries": [
                  "-lm"
               ]
            },
       }
   ]
}

params.cpp

#include <node.h>
#include <iostream>
#include <stdio.h>

extern "C" {
    #include "headerV6.h"
}

using namespace v8;
using namespace std;

void getParams(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    int status = 0, numplaca = 0;
    unsigned char json[9999] = ""; //<-- the lib function needs this (I know)
    unsigned char flagTuner = 0;
    int timeOut = 20; //segundos

    parametros(json, flagTuner, numplaca, timeOut, &status); // <-- this uses pow

    std::string sJson(reinterpret_cast<char*>(json));

    Local<String> retval = String::NewFromUtf8(isolate, sJson.c_str());
    args.GetReturnValue().Set(retval);
}

void Initialize(Local<Object> exports) {
   NODE_SET_METHOD(exports, "getRF", getParams);
}

NODE_MODULE(octotuner, Initialize);

teste.js

const rf = require('./build/Release/octotuner');
console.log(rf.getRF())

测试

sudo node teste.js

输出:

{
        "Numero_da_Placa":0,
        "Comando_OK":1,
        "Tuner0":{
                "Canal_Fisico":25,
                "QUALIDADE_LA":"-",
                "QUALIDADE_LB":"-",
                "QUALIDADE_LC":"-",
                "BER_LA":"-",
                "BER_LB":"-",
                "BER_LC":"-",
                "Potencia":-1.95,
                "SNR":3.9,
                "Modo":"8K",
                "Intervalo_de_Guarda":"1/8",
                "Modulacao_LA":"QPSK",
                "Taxa_do_Codigo_LA":"2/3",
                "Entrelacamento_LA":400,
                "Segmentos_LA":1,
                "Modulacao_LB":"64-QAM",
                "Taxa_do_Codigo_LB":"3/4",
                "Entrelacamento_LB":200,
                "Segmentos_LB":3,
                "Modulacao_LC":"Error",
                "Taxa_do_Codigo_LC":"Error",
                "Entrelacamento_LC":"Error",
                "Segmentos_LC":"Error"
        }
}

使用相同库的C程序(不在插件中,仅用于测试):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "headerV6.h"

int main(int argc, char *argv[]){

    int i = 0, status = 0, numplaca = 0;
    char json[9999] = {0};
    unsigned char flagTuner = 0;
    int timeOut = 20; //segundos

    parametros(json, flagTuner, numplaca, timeOut, &status);

    printf("%s", json);

    return 0;
}

编译为: gcc params.c libmackrnp6_0_2_fPIC.a -o teste

编译失败这就是为什么我认为插件的问题在于它没有链接数学库

(metadados.o): na função `recoverDataNum':
metadados.c:(.text+0x138): referência indefinida para `pow'
metadados.c:(.text+0x1d8): referência indefinida para `pow'
collect2: error: ld returned 1 exit status

编译为: gcc params.c libmackrnp6_0_2_fPIC.a -o teste -lm

编译成功

测试中: sudo ./teste

{
        "Numero_da_Placa":0,
        "Comando_OK":1,
        "Tuner0":{
                "Canal_Fisico":25,
                "QUALIDADE_LA":100.0,
                "QUALIDADE_LB":100.0,
                "QUALIDADE_LC":"-",
                "BER_LA":0.0000e+00,
                "BER_LB":0.0000e+00,
                "BER_LC":"-",
                "Potencia":-19.50,
                "SNR":37.9,
                "Modo":"8K",
                "Intervalo_de_Guarda":"1/8",
                "Modulacao_LA":"QPSK",
                "Taxa_do_Codigo_LA":"2/3",
                "Entrelacamento_LA":400,
                "Segmentos_LA":1,
                "Modulacao_LB":"64-QAM",
                "Taxa_do_Codigo_LB":"3/4",
                "Entrelacamento_LB":200,
                "Segmentos_LB":12,
                "Modulacao_LC":"-",
                "Taxa_do_Codigo_LC":"-",
                "Entrelacamento_LC":"-",
                "Segmentos_LC":"-"
        }
}

Solved

我使用了以下 binding.gyp 并且我得到了新版本的第三方零件库,现在可以使用了!谢谢

{
   "targets": [
       {
           "target_name": "octotuner",
           "sources": [
                "params.cpp"
            ],
            "include_dirs": [
                "./so"
            ],
            "link_settings": {
                 "libraries": ["/home/nvidia/webrf/api/so/libmackrnp6_0_2_fPIC.a","-lm"],
            }
       }
   ]
}