我想在javascript(node.js)中使用c / c ++语言,所以我使用addon,node-gyp。
我创建 spi.cc
(源文件),此文件在 /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)
我制作 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
然后我看了这个错误。
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
我认为,我的 spi.cc
文件找不到v8.h和node.h.
我用覆盆子pi。我从未在linux中使用过c / c ++语言。
答案 0 :(得分:0)
首先,请查看错误说明...您有一些拼写错误,特别是Valude
而不是Value
和Handel
而不是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
的值相匹配。