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

推荐订阅源

D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
博客园 - 聂微东
罗磊的独立博客
W
WeLiveSecurity
博客园_首页
Scott Helme
Scott Helme
V
Visual Studio Blog
T
The Exploit Database - CXSecurity.com
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
L
Lohrmann on Cybersecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
F
Full Disclosure
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
雷峰网
雷峰网
博客园 - 【当耐特】
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
T
Tor Project blog
V
V2EX
爱范儿
爱范儿
C
Check Point Blog
T
Threatpost
Project Zero
Project Zero
量子位
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
I
Intezer
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 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 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 WHCA 白宫记者协会 All In One Pascal Editor All In One 主流犬种图解指南 All In One 泡沫喷雾 & 辣椒喷雾 All In One 如何给身份证照片添加水印 All In One GoPro MISSION 1 PRO price All In One 杭州历史天气数据 All In One Pandoc All In One GoPro MISSION 1 SERIES All In One GoPro telemetry 中的 GPS5 与 GPS9 是什么 All In One xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs xgqfrms, cnblogs, blogs OpenCode All In One OpenClaw 设置 cron 定时任务 All In One free MongoDB Cloud API All In One 如何在 Raspberry Pi 安装 OpenClaw All In One free cloud LLM models API All In One Claude Code Free Video Tutorials All In One 如何解决 OpenClaw 升级后导致 feishu plugin 无法使用的问题 All In One Claude Code skills & plugins All In One LLM Benchmark All In One How to fix use the FileZilla FTP upload file error 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, 禁止转载 🈲️,侵权必究⚠️!