从Node.js调用C ++库(节点插件/node-ffi) [英] Call C++ library from Node.js (Node addons / node-ffi)

查看:530
本文介绍了从Node.js调用C ++库(节点插件/node-ffi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个外部C ++库(我可以访问.so文件以及头文件)集成到我的Node.js应用程序中.

I'm trying to integrate an external C++ library (I have access to the .so file as well as the header files) into my Node.js application.

经过大量研究,我的选择减少到:

After a lot of research my options are reduced to:

  1. 编写节点插件

使用 node-ffi

根据node-ffi的gitHub的定义,我无法确定它是否可以直接在C ++库上运行:

From node-ffi's gitHub's definition I can't tell if it will or will not work directly on C++ libraries:

node-ffi是一个Node.js插件,用于使用纯JavaScript加载和调用动态库.它可以用于创建与本机库的绑定,而无需编写任何C ++代码.

node-ffi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code.

所以我的问题是:

  • 选项1)是否暗示以某种方式重写外部C ++库?
  • node-ffi是否可以直接调用C ++库而无需编写任何类型的C包装器?

在C/C ++方面,我不是专家,因此,如果我错过了一些基本的知识以使您能够回答,请告诉我,以便我改善问题.

I'm no expert when it comes to C/C++ so if I missed something basic for you to be able to answer please let me know so I can improve my question.

推荐答案

node-ffi似乎主要用于C程序.我在上周经历了这一点,并发现节点插件的运气要好得多.您要做的是在库中的C ++代码和node.js之间编写垫片.在我的情况下,我需要为安全协议编码和解码数据包,因此我制作了包含数据包的节点缓冲区,并编写了将数据从缓冲区中取出的C ++代码,然后将数据发送到编码并解码的C代码中.解码的数据包.

node-ffi seems to be primarily for C programs. I went through this in the last week, and found much better luck with node addons. What you have to do is write a shim between the C++ code in the library and node.js. In my case, I needed to encode and decode packets for a security protocol, so I made node buffers that contained the packets, and wrote C++ code that got the data out of the buffers, then send the data to my C code that encoded and decoded packets.

此页面: http://luismreis.github.io/node -bindings-guide/docs/returning.html 提供了一些很棒的示例,说明了如何在C ++中将数据放入和取出node.js缓冲区.

This page: http://luismreis.github.io/node-bindings-guide/docs/returning.html has some great examples of how to get data in and out of node.js buffers in C++.

这篇关于从Node.js调用C ++库(节点插件/node-ffi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆