0

I want to use c/c++ language in javascript(node.js), so I use addon, node-gyp.
And I make spi.cc(source file), this file make in /home/pi

  GNU nano 2.2.6               File: spi.cc                                     

#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<wiringPi.h>
#include<wiringPiSPI.h>
#include<node.h>
#include<v8.h>

#define CS_MCP3208 10
#define SPI_CHANNEL 0
#define SPI_SPEED 1000000

using namespace v8;

int read_mcp3208_adc(unsigned char adcChannel)
{
        unsigned char buff[3];
        int adcValue = 0 ;
        buff[0] = 0x06 |((adcChannel & 0x07 ) >>2);
        buff[1] = ((adcChannel & 0x07) << 6);
        buff[2] = 0x00;

        digitalWrite(CS_MCP3208,0);
        wiringPiSPIDataRW(SPI_CHANNEL,buff,3);
        buff[1] = 0x0F & buff[1];
        adcValue = (buff[1] << 8)| buff[2];
        digitalWrite(CS_MCP3208,1);

        return adcValue;
}
Handle<Valude> GetValue(const Arguments& args)
{
        HandleScope scope;
        int adcValue = 0;
        float temp = 0.0;

        wiringPiSetup();
        wiringPiSPISetup(SPI_CHANNEL,SPI_SPEED);

        temp = (read_mcp3208_adc(0)/24.818)-40;

        return scope.Close(Number::New(temp);
}
void Init(Handel<Object> exports)
{
        exports->Set(String::NewSymbol("getValue"),FunctionTemplate::New(GetVal$
}

NODE_MODULE(myaddon, Init)

And I make binding.gyp, this file make in /home/pi,too.

  GNU nano 2.2.6             File: binding.gyp                                  

{
        "targets":[
        {
                "target_name": "spi",
                "sources": ["./spi.cc"]
        }
        ]
}

I use node-gyp build after I finish node-gyp configure
Then I watch the this error.

root@raspberrypi:/home/pi# node-gyp build
gyp info it worked if it ends with ok
gyp info using node-gyp@1.0.2
gyp info using node@0.10.32 | linux | arm
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/home/pi/build'
  CXX(target) Release/obj.target/spi/spi.o
../spi.cc:31:8: error: ‘Valude’ was not declared in this scope
../spi.cc:31:14: error: template argument 1 is invalid
../spi.cc: In function ‘int GetValue(const v8::Arguments&)’:
../spi.cc:42:38: error: expected ‘)’ before ‘;’ token
../spi.cc:34:6: warning: unused variable ‘adcValue’ [-Wunused-variable]
../spi.cc: At global scope:
../spi.cc:44:11: error: variable or field ‘Init’ declared void
../spi.cc:44:11: error: ‘Handel’ was not declared in this scope
../spi.cc:44:24: error: expected primary-expression before ‘>’ token
../spi.cc:44:26: error: ‘exports’ was not declared in this scope
../spi.cc:49:1: error: ‘Init’ was not declared in this scope
../spi.cc:49:1: note: suggested alternative:
/root/.node-gyp/0.10.32/src/node.h:93:8: note:   ‘node::Init’
../spi.cc: In function ‘int GetValue(const v8::Arguments&)’:
../spi.cc:43:1: warning: control reaches end of non-void function [-Wreturn-type]
spi.target.mk:82: recipe for target 'Release/obj.target/spi/spi.o' failed
make: *** [Release/obj.target/spi/spi.o] Error 1
make: Leaving directory '/home/pi/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack     at ChildProcess.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:810:12)
gyp ERR! System Linux 3.12.28+
gyp ERR! command "node" "/usr/local/bin/node-gyp" "build"
gyp ERR! cwd /home/pi
gyp ERR! node -v v0.10.32
gyp ERR! node-gyp -v v1.0.2
gyp ERR! not ok 

I think, my spi.cc file don't find v8.h and node.h.
I use raspberry pi. I have never used c/c++ language in linux.

ahang
  • 51
  • 1
  • 3

1 Answers1

0

First of all, look at the error descriptions... you have some typos, notably Valude instead of Value and Handel instead of Handle.

Secondly, you might wrap your Init() function and NODE_MODULE() like this:

extern "C" {
  void Init(Handle<Object> exports)
  {
    exports->Set(String::NewSymbol("getValue"),FunctionTemplate::New(GetVal...
    NODE_MODULE(myaddon, Init)
  }

Lastly, the value you supply as the first argument to NODE_MODULE() must match what you have for target_name in your binding.gyp.

mscdex
  • 104,356
  • 15
  • 192
  • 153