In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to write nodejs C++ plug-ins". In daily operation, I believe many people have doubts about how to write nodejs C++ plug-ins. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to write nodejs C++ plug-ins". Next, please follow the editor to study!
Environment
1 ubuntu18.04 .
2 install nodejs v12 and npm install node-gyp-g.
Write code
Write an example of a test. Test.cc
/ / hello.cc using N-API
# include
Namespace demo {
Napi_value Method (napi_env env, napi_callback_info args) {
Napi_value greeting
Napi_status status
Status = napi_create_string_utf8 (env, "world", NAPI_AUTO_LENGTH, & greeting)
If (status! = napi_ok) return nullptr
Return greeting
}
Napi_value init (napi_env env, napi_value exports) {
Napi_status status
Napi_value fn
Status = napi_create_function (env, nullptr, 0, Method, nullptr, & fn)
If (status! = napi_ok) return nullptr
Status = napi_set_named_property (env, exports, "hello", fn)
If (status! = napi_ok) return nullptr
Return exports
}
NAPI_MODULE (NODE_GYP_MODULE_NAME, init)
} / / namespace demo
Binding.gyp
{
"targets": [
{
"target_name": "test"
"sources": [". / test.cc"]
}
]
}
Then execute npm init. The contents are as follows
{
"name": "test-addons"
"version": "1.0.1"
"description":
Main: ". / build/Release/test.node"
"scripts": {
"test": "echo\" Error: no test specified\ "& exit 1"
"install": "node-gyp rebuild"
}
"author":
"license": "ISC"
"gypfile": true
}
We mainly look at two configurations related to plug-ins.
Main: ". / build/Release/test.node"
"gypfile": true
Main defines the entry of the plug-in because we do not provide users with the api of js. Instead, the .node file is provided directly, so the path defined here is the path of the plug-in. Gypfile tag this package is a C++ plug-in, when the user installs the package using npm, it executes node-gyp rebuild and generates the corresponding binaries for the user's system environment. Some articles describe the need to add "install" to the value of scripts: "node-gyp rebuild". The npm documentation says that this rebuild operation will be performed by default if there is a .gyp file under the package.
"scripts": {"install": "node-gyp rebuild"}
If there is a binding.gyp file in the root of your package and you have not defined an install or preinstall script, npm will default the install command to compile using node-gyp.
When everything is ready, we can execute the npm publish release (remember to log in to npm first).
test
Let's install this package first. Npm install test-addons-- unsafe-perm, and then write code to test it.
Var addon = require ("test-addons/build/Release/test")
Console.log (addon.hello ())
We found that this was obviously troublesome. So we'd better provide users with the interface of the js module in the package. We add index.js to the bag.
Var addon = require (". / build/Release/test")
Module.exports = addon
Modify the test code
Var addon = require ("test-addons")
Console.log (addon.hello ()); at this point, the study on "how to write nodejs C++ plug-ins" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.