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

推荐订阅源

B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
S
Schneier on Security
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
Security Latest
Security Latest
Jina AI
Jina AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recorded Future
Recorded Future
T
Tor Project blog
有赞技术团队
有赞技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
C
Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
IT之家
IT之家
T
Threatpost
Cyberwarzone
Cyberwarzone
O
OpenAI News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
月光博客
月光博客
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 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, 禁止转载 🈲️,侵权必究⚠️!