How to compile nodejs extension in C ++

Source: Internet
Author: User

Before writing node. js extensions in C ++, I used PHP to write IP address-Based IP address queries. Later I used C language to write extensions, which greatly improved the efficiency and then converted to PHP extensions. I remembered that I used a mobile phone number to query the number of the Region. Recently I was studying nodejs, so I used C ++ to compile the node. js extension. there are indeed many problems encountered. Record them for your reference: 1. character encoding problem: nodejs does not support gbk encoding well. To improve program efficiency, first convert the IP address attribution resources to UTF-8 encoding. Specifically, use the PHP script foreach to use iconv for conversion 2. the parameter transfer problem is that many programs write a C ++ Implementation of the node. js extension of helloworld, but there is no parameter transfer here. Refer to the node. js official website and the int type is always not found, how can I convert javascript to C ++? In the spirit of finding a girl in difficulty, I found it: Using args [0]-> Int32Value () can be converted to 3. the specific compilation process is as follows:. write binding. gyp, first write the simplest Configuration:

{
'Targets ':[
{
'TargetName': 'mtc'
'Source': ['mtc. CC']
}
]
}
Target
Name is the module name, which is used for require.
Source is the name of the C ++ source file in the same directory as binding. gyp.

B. Compile the source code of C ++:
#include 
 
  #include 
  
   using namespace v8;const int MaxCityLength = 25;
  
 

Char * Search (const char * fileName, const int & number );
Class Info {
Public:
Info (): mBefore (0), mAfter (0), m_cityIndex (0 ){

}Info(int begin, unsigned short skip, unsigned short city_index){    setBegin(begin);    setSkip(skip);    m_cityIndex = city_index;}int getBegin() {    int lastTwo = m_after - getNumberExceptLastTwo()*100;    return m_before * 100 + lastTwo;}unsigned short getNumberExceptLastTwo() {    return m_after * 0.01;}unsigned short getCityIndex() {    return m_cityIndex;}unsigned short getLastTwo(int number) {    int exceptLastTwoNum = number * 0.01;    return (number - exceptLastTwoNum * 100);}void setBegin(int& number) {    int lastTwo = getLastTwo(number);    m_before = number * 0.01;    m_after = getNumberExceptLastTwo();}void setSkip(unsigned short skip) {    m_after = getNumberExceptLastTwo() * 100 + getLastTwo(m_after);}void setCityIndex(unsigned short& city) {    m_cityIndex = city;}char* FindResult(FILE* file, const int& count, Info info){    int totalOffset = sizeof(int) + count*sizeof(Info) + info.getCityIndex()* MaxCityLength;    fseek(file, totalOffset,SEEK_SET);    char* location = new char [MaxCityLength];    fread(location, MaxCityLength, 1, file);    fclose(file);    return location;}

Private:
Unsigned short mBefore;
Unsigned short m
After;
Unsigned short m_cityIndex;

};

Char * search (const char * fileName, const int & number ){
FILE * file = 0;
File = fopen (fileName, "rb ");
If (file = 0 ){
Return (char *)"";

}int count = 0;fread(&count ,sizeof(int), 1, file);int left = 0, right = count - 1;Info info;    while(left < right) {    int middle = (left + right)/2;    fseek(file, sizeof(int) + middle * 6 ,SEEK_SET);    fread(&info ,sizeof(unsigned short)*3, 1, file);    if(number < info.getBegin()) {        right =middle -1;    } else if(number >info.getBegin() + info.getNumberExceptLastTwo()) {        left = middle +1;    } else {        return info.FindResult(file, count,info);    }}return (char*) "";

}

Handle mtc (const Arguments & args ){
HandleScope scope;
Char * ret = search ("AreaData. dat", args [0]-> Int32Value ());
Return scope. Close (String: New (char *) ret ));

}

Void init (HandleTarget ){
NODESETMETHOD (target, "m2c", mtc );
}

NODE_MODULE (mtc, init );

C. You can use the following two commands:
node-gyp configure node-gyp build

Finally, the mtc. node file is generated:

Write test. js
Var mtc = require ("mtc ');
Console. log ('mtc. mtc () = ', mtc. mtc (1895926 ))
Execute: node test. js will output

Download

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.