簡體   English   中英

node-gyp構建時出錯

[英]error while node-gyp build

我想在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 configure后,我使用node-gyp build
然后我看這個錯誤。

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。
我用樹莓派。 我從未在Linux中使用過c / c ++語言。

首先,查看錯誤描述...您有一些錯別字,尤其是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()的第一個參數提供的值必須與NODE_MODULE()target_name的值匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM