c++node.jsnode.js-addon

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


I'm new in developing nodejs addons in C++.

I have a static c lib that I include in my addon, the lib is needed to talk to a custom hardware, but some functions of this library uses math.h. So when I compile in a C example I do: gcc main.c libmackrnp6_0_2_fPIC.a -o test -lm, no problem there, but when I include this lib in the cpp addon I have a problem with the pow function.

How can I compile the addon sudo node-gyp configure build with the -lm option (gcc equivalent)?

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())

Testing

sudo node teste.js

Output:

{
        "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 program that uses the same library (not in addon, just for testing):

#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;
}

Compiled with: gcc params.c libmackrnp6_0_2_fPIC.a -o teste

Compile FAIL (that's why I think the problem with the addon is that it's not linking math lib)

(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

Compiled with: gcc params.c libmackrnp6_0_2_fPIC.a -o teste -lm

Compile OK

Testing: 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":"-"
        }
}

Solution

  • Solved

    I've used the following binding.gyp and I've got a new version of the thirdy part library and it's working now! Thanks.

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