2014-10-09 62 views
0

我想在javascript(node.js)中使用c/C++語言,所以我使用addon,node-gyp。
我做spi.cc(源文件),該文件使/home/pi錯誤while node-gyp build

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) 

我做binding.gyp,這個文件使/home/pi了。

GNU nano 2.2.6    File: binding.gyp         

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

我用node-gyp build我完成node-gyp configure
後,然後我看這個錯誤。

[email protected]:/home/pi# node-gyp build 
gyp info it worked if it ends with ok 
gyp info using [email protected] 
gyp info using [email protected] | 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 

我想,我的spi.cc文件沒有找到v8.h和node.h.
我使用覆盆子pi。我從來沒有在Linux中使用過C/C++語言。

回答

0

首先,看看錯誤描述......你有一些錯別字,尤其是Valude代替ValueHandel而不是Handle

其次,你可能會包住Init()功能NODE_MODULE()這樣的:

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

最後,您提供的第一個參數NODE_MODULE()值必須與你有什麼在你binding.gyp target_name