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

标签 c++ node.js node.js-addon

我是用 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

绑定(bind).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"],
            }
       }
   ]
}

关于c++ - NodeJS : node-gyp compile with the equivalent gcc -lm option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59849758/

相关文章:

c++ - 偏最小二乘库

c++ - 有谁知道在执行 SDL 程序时如何消除 stdout.txt 和 stderr.txt 的创建

javascript - 使用jQuery with/Electron/NodeJS匹配一组数字以替换为文本

javascript - 我应该在 XMLHttpRequest 中使用什么回调函数来呈现响应?

c++ - node.js 插件 - 如何将字符串参数传递给 (nan) C++

c++ - 我如何让连续内存以安全的方式表现出多态性?

javascript - 如何从 Sails.js 中的此方法返回 n 的值

javascript - v8 回调不适用于堆上的 JavaScript 对象

c++ - 如何检查对象是否是 Nan 2 中构造函数的实例?

c++ - 使用 clang 从 C++ 编译到 LLVM IR 时指定多个文件