惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
J
Java Code Geeks
博客园 - 聂微东
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
腾讯CDC
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
美团技术团队
博客园 - Franky
Google DeepMind News
Google DeepMind News
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
The Cloudflare Blog

博客园 - xgqfrms

xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs Remotion Video Maker All In One git worktree All In One Tesla 的车机使用什么技术来渲染汽车模型的? 宜家 VEVELSTAD 维维斯托床架 All In One macOS sysmond bug All In One Three.js All In One The COF of LCD Monitor All In One Dell 显示器 S2419HM 灰屏 &花屏 All In One AI Harness Engineering All In One 电脑外接显示器天梯榜 All In One How to change the speed display unit of GSP from mph to km/h using GoPro Labs All In One
xgqfrms, cnblogs, blogs
xgqfrms · 2026-07-01 · via 博客园 - xgqfrms

Node.js Native Addons All In One

Node.js Native Addons/Node.js 原生扩展插件, 是指用 C / C++ 等底层语言编写的模块,然后通过 Node.js 提供的接口编译成可被 JavaScript 直接调用的扩展插件

C++ Addons

Addons are dynamically-linked shared objects that can be loaded via the require() function as ordinary Node.js modules.
Addons provide a foreign function interface between JavaScript and native code.

There are three options for implementing addons:

  1. Node-API (recommended ✅)
  2. nan (Native Abstractions for Node.js)
  3. direct use of internal V8, libuv, and Node.js libraries

https://nodejs.org/api/addons.html

Node-API

Node-API(原名 N-API)是一个用于构建原生插件的 API。它独立于底层 JavaScript 运行时(例如 V8),并作为 Node.js 的一部分进行维护。
该 API 将在各个 Node.js 版本之间保持应用程序二进制接口 (ABI) 的稳定性。
其目的是使插件免受底层 JavaScript 引擎变更的影响,并允许为某一主要版本编译的模块无需重新编译即可在后续的 Node.js 主要版本上运行。ABI 稳定性指南提供了更详细的解释。

https://nodejs.org/api/n-api.html

Node-API is a stable C API built into Node.js that lets C/C++ code create, read, and manipulate JavaScript values as if they were created by JavaScript itself.
It was introduced experimentally in Node.js 8.0.0 and became stable (no longer behind a flag) in the Node.js 8/10 timeframe.

Because Node-API is part of Node.js itself, it requires no additional installation.

image
image

https://nodejs.org/learn/node-api

https://nodejs.org/zh-cn/learn/node-api

Node-API 的一个重要搭档是 npm 包 node-addon-api
它将 C API 封装在一个符合 C++ 惯用语的 C++ 对象模型中,减少了样板代码,并使一些常见模式(例如将 C++ 对象封装成 JavaScript 对象)更加符合人体工程学。
它保留了 Node-API 的全部 ABI 稳定性保证。

$ npm i node-addon-api

https://www.npmjs.com/package/node-addon-api

https://github.com/nodejs/node-addon-api

NAN

Native Abstractions for Node.js / Node.js 的原生抽象

https://github.com/nodejs/nan

demos

// the equivalent of the following JavaScript code
module.exports.hello = () => 'world';

hello.cc

#include <node.h>

namespace demo {

  using v8::FunctionCallbackInfo;
  using v8::Isolate;
  using v8::Local;
  using v8::NewStringType;
  using v8::Object;
  using v8::String;
  using v8::Value;

  void Method(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    args.GetReturnValue().Set(String::NewFromUtf8(
        isolate, "world", NewStringType::kNormal).ToLocalChecked());
  }

  void Initialize(Local<Object> exports) {
    NODE_SET_METHOD(exports, "hello", Method);
  }

  // N.B.: no semi-colon, this is not a function
  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) 

}
// namespace demo


Makefile, hello.cc => > hello.node

NODEJS_DEV_ROOT ?= $(shell dirname "$(command -v node)")/..
CXXFLAGS = -std=c++23 -I$(NODEJS_DEV_ROOT)/include/node -fPIC -shared -Wl,-undefined,dynamic_lookup

# target source
hello.node: hello.cc
    $(CXX) $(CXXFLAGS) -o $@ $<

$ make
$ node -p 'require("./hello.node").hello()'
# world


.cpp vs .cc

.cpp 和 .cc 本质上没有语言层面的区别,只是 C++ 源文件不同命名习惯

image

  • C Plus Plus, .cpp
  • Google C++ Style Guide, .cc
$ g++ main.cpp
# or
$ g++ main.cc

CMake

image

C files → compiled with C compiler (e.g. gcc)
C++ files → compiled with C++ compiler (e.g. g++ / clang++)

refs

image

image



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!