gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:24 24 4
gpt4 key购买 nike

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

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