Use C ++ to write components for nodejs to improve node processing efficiency
Source: Internet
Author: User
Yesterday I studied how to use C ++ to interact with node. In the node program, if there is a large amount of data computing, the processing is slow, and C ++ can be used for processing, then, the callback is returned to the node. First, let's take a look at how node interacts with C ++. Prerequisites: required... syntaxHighlighter. all () Yesterday I studied how to use C ++ to interact with node. In the node program, if there is a large amount of data to be calculated, the processing is slow, you can use C ++ for processing, and then return it to node through callback (in the form of callback. First, let's take a look at how node interacts with C ++. Prerequisites: You must install the nodejs environment and the node-gyp package. Npm is used for installation. This is too convenient. Modify the package. json dependency option in this folder and run npm install. 1. take "hello world" as an example. 1) create a folder "hello" and add three files in the folder, "hello. cc, binding. gyp, test. js (where hello. cc is a c ++ source program file, binding. gyp is the configuration file of the compilation module, which is compiled by the node-gyp program, test. js is the call program of nodejs .) The file structure is as follows: hello. cc code: Copy Code # include # Include Using namespace v8; Handle Method (const Arguments & args) {HandleScope scope; return scope. Close (String: New ("hello, world");} void init (Handle Exports) {exports-> Set (String: NewSymbol ("hello"), FunctionTemplate: New (Method)-> GetFunction ();} NODE_MODULE (hello, init) copy the code binding. gyp is as follows: copy the code {"targets": [{"target_name": "hello", "sources": ["hello. cc "]} copy the code test. js: var addon = require ('. /build/Release/hello '); console. log (addon. hello (); 2) directly execute node-gyp configure build to directly compile. 3) Run node test. js to output the result. Okay, that's easy. Node can directly call programs written in C ++. Explanation of the above program: In hello. in cc, we first created a function Method, which returns a "hello, world" string, and then we created an init function as an initialization function, we called a function at the end. We bound this module to NODE_MODULE (hello, init) and pointed out on the official website that all node plug-ins must output an initialization function, that is to say, the following code is required in each module and is in a fixed format. Void Initialize (HandleExports); NODE_MODULE (module_name, Initialize) Where module_name must correspond to target_name in binding. gyp. After node-gyp configure build compilation, a new build folder is generated under the current file. By referencing the build result in test. js, we can call the program written in C ++. OK. A simple node and C ++ program are all written. Now, the node can interact with the program written in C ++ and directly call the program written in C ++. If the node cannot be processed, it can be processed through C ++. 2. node uses the callback method to call the C ++ processing result and return it to node 1) according to the above method, create three files callback first. cc, binding. gyp, test. js callback. cc: copy the Code # define BUILDING_NODE_EXTENSION # include Using namespace v8; Handle RunCallback (const Arguments & args) {HandleScope scope; Local Cb = Local : Cast (args [0]); const unsigned argc = 1; Local Argv [argc] = {Local : New (String: New ("hello world")}; cb-> Call (Context: GetCurrent ()-> Global (), argc, argv ); return scope. close (Undefined ();} void Init (Handle Exports, HandleModule) {module-> Set (String: NewSymbol ("exports"), FunctionTemplate: New (RunCallback)-> GetFunction ();} NODE_MODULE (callback, Init) copy the code binding. gyp: copy the code {"targets": [{"target_name": "callback", "sources": ["callback. cc "]} copy the code test. js: var addon = require ('. /build/Release/callback '); addon (function (msg) {console. log (msg); // 'Hello world'}); 2) Compile and enter node-gyp configure build on the terminal, A new build file will be generated in the current directory. 3) test, node test. js output // hello world
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service