node-gyp build error "calling a protected constructor of class 'v8::HandleScope'" on mac - c++

I use node-gyp to build my c++ library for node, and copy some examples on the web.
callback.cc
#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const v8::internal::Arguments& args){
HandleScope scope;
return scope.Close(String::New("hello,world"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
binding.gyp
{
"targets": [
{
"target_name": "hello",
"sources": [ "callback.cc" ]
}
]
}
run node-gyp configure ok.
run node-gyp build fail, error:
gyp info it worked if it ends with ok
gyp info using node-gyp#3.6.0
gyp info using node#7.9.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/hello/callback.o
../callback.cc:5:15: error: calling a protected constructor of class 'v8::HandleScope'
HandleScope scope;
^
/Users/zhouweiming/.node-gyp/7.9.0/include/node/v8.h:908:13: note: declared protected here
V8_INLINE HandleScope() {}
^
../callback.cc:6:16: error: no member named 'Close' in 'v8::HandleScope'
return scope.Close(String::New("hello,world"));
~~~~~ ^
../callback.cc:6:30: error: no member named 'New' in 'v8::String'
return scope.Close(String::New("hello,world"));
~~~~~~~~^
../callback.cc:9:65: error: cannot initialize a parameter of type 'v8::Isolate *' with an lvalue of type 'Handle<v8::Value> (const v8::internal::Arguments &)'
(aka 'v8::Local<v8::Value> (const v8::internal::Arguments &)')
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
^~~~~~
/Users/zhouweiming/.node-gyp/7.9.0/include/node/v8.h:5079:16: note: passing argument to parameter 'isolate' here
Isolate* isolate, FunctionCallback callback = 0,
^
../callback.cc:9:24: error: no member named 'NewSymbol' in 'v8::String'
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
~~~~~~~~^
5 errors generated.
make: *** [Release/obj.target/hello/callback.o] Error 1
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_npminstall/node_modules/.node- gyp#3.6.0/lib/build.js:285:23)
gyp ERR! stack at emitTwo (events.js:106:13)
gyp ERR! stack at ChildProcess.emit (events.js:194:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Darwin 16.5.0
gyp ERR! command "/usr/local/Cellar/node/6.9.1/bin/node" "/usr/local/bin/node-gyp" "build"
gyp ERR! cwd /Users/zhouweiming/data/project/electron-projects/dll
gyp ERR! node -v v7.9.0
gyp ERR! node-gyp -v v3.6.0
gyp ERR! not ok
I'm not a c++ coder, please help me.
node: 7.9.0
node-gyp: 3.6.0
xcode: 8.1
mac: 10.12.4
gcc:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I have solved it. Because the c++ code in demo is outdated.
the new codes like:
#include <node/v8.h>
#include <node/node.h>
using namespace node;
using namespace v8;
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "Hello world"));
}
void init(v8::Local<v8::Object> target) {
NODE_SET_METHOD(target, "hello", Method);
}
NODE_MODULE(binding, init);
there are too many outdated examples on the web, I will keep this question

Related

Errors from `node-gyp build`: invalid value 'gnu++1y' in '-std=gnu++1y'

UPDATE. Output of gcc -v as requested:
→ gcc -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
I am trying to follow the Node.js official addons example
The node-gyp configure step runs without errors.
However, the following node-gyp build step is failing:
→ node-gyp build
gyp info it worked if it ends with ok
gyp info using node-gyp#5.0.2
gyp info using node#10.16.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/addon/hello.o
error: invalid value 'gnu++1y' in '-std=gnu++1y'
make: *** [Release/obj.target/addon/hello.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/Users/dmitrizaitsev/.npm-global/lib/node_modules/node-gyp/lib/build.js:190:23)
gyp ERR! stack at ChildProcess.emit (events.js:198:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Darwin 12.6.0
gyp ERR! command "/usr/local/bin/node" "/Users/dmitrizaitsev/.npm-global/bin/node-gyp" "build"
gyp ERR! cwd /Users/dmitrizaitsev/test/addons/official
gyp ERR! node -v v10.16.0
gyp ERR! node-gyp -v v5.0.2
gyp ERR! not ok
The first error message is
invalid value 'gnu++1y' in '-std=gnu++1y'
What does it mean and how can this error possibly be fixed?

Node-gyp build fail. Can't locate library headerfile in sub-sub-directory of included directory

I want to use the DronecodeSDK (C++) library within a Node.js application. I'm currently unable to build the node-addon that includes the DronecodeSDK headers. It's only the headers within the sub-subdirectories that node-gyp can't find.
#include <dronecode_sdk/dronecode_sdk.h> is included just fine, but #include <dronecode_sdk/plugins/action/action.h> isn't.
If I move the action.h file to dronecode_sdk/action.h then the include works just fine. It's only when it's moved inside the subdirectory of plugins that it isn't found.
My binding.gyp file:
{
"targets": [
{
"include_dirs" : [
"<!(node -e \"require('nan')\")",
"../lib/DronecodeSDK/install/include"
],
"libraries": [ "../lib/DronecodeSDK/install/lib/libdronecode_sdk.a" ],
"target_name": "DronecoreSDK",
"sources": [ "DronecoreSDKBridge.cc" ]
}
]
}
Two include statements in 'DronecodeSDKBridge.cc' :
#include <dronecode_sdk/dronecode_sdk.h>
#include <dronecode_sdk/plugins/action/action.h>
I get the following error message:
gyp info it worked if it ends with ok
gyp info using node-gyp#3.8.0
gyp info using node#10.15.0 | darwin | x64
gyp info spawn /usr/local/bin/python2
gyp info spawn args [ '/usr/local/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/Users/aokholm/src/kitex/_3_GroundStation/electron-GC/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/usr/local/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/Users/aokholm/.node-gyp/10.15.0/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/Users/aokholm/.node-gyp/10.15.0',
gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/Users/aokholm/.node-gyp/10.15.0/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/Users/aokholm/src/kitex/_3_GroundStation/electron-GC',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/DronecoreSDK/DronecoreSDKBridge.o
../DronecoreSDKBridge.cc:10:10: fatal error: 'dronecode_sdk/plugins/action/action.h' file not found
#include <dronecode_sdk/plugins/action/action.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [Release/obj.target/DronecoreSDK/DronecoreSDKBridge.o] Error 1
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:262:23)
gyp ERR! stack at ChildProcess.emit (events.js:182:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Darwin 18.2.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/aokholm/src/kitex/_3_GroundStation/electron-GC
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Could it be that sub-subdirectories just doesn't work, or is the error somewhere else? I have checked that the file permissions of all the headers files are readable.
Node-gyp paths are quite mysterious.
The library path ../lib/DronecodeSDK/install/lib/libdronecode_sdk.a is correct. However the include path ../lib/DronecodeSDK/install/include was incorrect.
The reason why node-gyp could find the dronecode_sdk.h header anyway is that that it by default searches for headers in the two most top level directories of the project.
Changing the include path to lib/DronecodeSDK/install/include solved the problem.

Error installing ember-cli using 'nodesource/wheezy:5.0.0' using dockerfile

Hi I'm trying to install ember-cli based off 'nodesource/wheezy:5.0.0'
I'm getting the following error..would it be possible to fix this issue? or do I need to go back to older versions of node (0.12)?
Step 0 : FROM nodesource/wheezy:5.0.0
5.0.0: Pulling from nodesource/wheezy
1d53ee5b1800: Pull complete
74872ba2a3b0: Pull complete
292a0889fc25: Pull complete
d8e43051f9c0: Pull complete
34a3fa49705c: Pull complete
992d01b899f4: Pull complete
120d2b6ae4ba: Pull complete
651395105b63: Pull complete
bf87c0988c7e: Pull complete
38e00921614b: Pull complete
038db7a41985: Pull complete
Digest: sha256:ace6600dde6399fa02df5c03c2ecf6b3e7de4745142639d13f502a04c1e62fa4
Status: Downloaded newer image for nodesource/wheezy:5.0.0
---> 038db7a41985
Step 1 : RUN npm install -g bower ember-cli
---> Running in 6c97ac90d722
/usr/bin/bower -> /usr/lib/node_modules/bower/bin/bower
/usr/bin/ember -> /usr/lib/node_modules/ember-cli/bin/ember
> bufferutil#1.1.0 install /usr/lib/node_modules/ember-cli/node_modules/bufferutil
> node-gyp rebuild
gyp WARN EACCES user "undefined" does not have permission to access the dev dir "/root/.node-gyp/5.0.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/lib/node_modules/ember-cli/node_modules/bufferutil/.node-gyp"
make: Entering directory `/usr/lib/node_modules/ember-cli/node_modules/bufferutil/build'
CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
In file included from ../src/bufferutil.cc:16:0:
../../nan/nan.h:261:25: error: redefinition of 'template<class T> v8::Local<T> _NanEnsureLocal(v8::Local<T>)'
../../nan/nan.h:256:25: error: 'template<class T> v8::Local<T> _NanEnsureLocal(v8::Handle<T>)' previously declared here
../../nan/nan.h:661:13: error: 'node::smalloc' has not been declared
../../nan/nan.h:661:35: error: expected ',' or '...' before 'callback'
../../nan/nan.h: In function 'v8::Local<v8::Object> NanNewBufferHandle(char*, size_t, int)':
../../nan/nan.h:665:50: error: 'callback' was not declared in this scope
../../nan/nan.h:665:60: error: 'hint' was not declared in this scope
../../nan/nan.h: In function 'v8::Local<v8::Object> NanNewBufferHandle(const char*, uint32_t)':
../../nan/nan.h:672:67: error: call of overloaded 'New(v8::Isolate*, const char*&, uint32_t&)' is ambiguous
../../nan/nan.h:672:67: note: candidates are:
In file included from ../src/bufferutil.cc:10:0:
/usr/lib/node_modules/ember-cli/node_modules/bufferutil/.node-gyp/5.0.0/include/node/node_buffer.h:31:40: note: v8::MaybeLocal<v8::Object> node::Buffer::New(v8::Isolate*, v8::Local<v8::String>, node::encoding) <near match>
/usr/lib/node_modules/ember-cli/node_modules/bufferutil/.node-gyp/5.0.0/include/node/node_buffer.h:31:40: note: no known conversion for argument 3 from 'uint32_t {aka unsigned int}' to 'node::encoding'
/usr/lib/node_modules/ember-cli/node_modules/bufferutil/.node-gyp/5.0.0/include/node/node_buffer.h:43:40: note: v8::MaybeLocal<v8::Object> node::Buffer::New(v8::Isolate*, char*, size_t) <near match>
/usr/lib/node_modules/ember-cli/node_modules/bufferutil/.node-gyp/5.0.0/include/node/node_buffer.h:43:40: note: no known conversion for argument 2 from 'const char*' to 'char*'
In file included from ../src/bufferutil.cc:16:0:
../../nan/nan.h: In function 'v8::Local<v8::Object> NanNewBufferHandle(uint32_t)':
../../nan/nan.h:676:61: error: could not convert 'node::Buffer::New(v8::Isolate::GetCurrent(), ((size_t)size))' from 'v8::MaybeLocal<v8::Object>' to 'v8::Local<v8::Object>'
../../nan/nan.h: In function 'v8::Local<v8::Object> NanBufferUse(char*, uint32_t)':
../../nan/nan.h:683:12: error: 'Use' is not a member of 'node::Buffer'
make: *** [Release/obj.target/bufferutil/src/bufferutil.o] Error 1
make: Leaving directory `/usr/lib/node_modules/ember-cli/node_modules/bufferutil/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:270:23)
gyp ERR! stack at emitTwo (events.js:87:13)
gyp ERR! stack at ChildProcess.emit (events.js:172:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Linux 4.1.10-boot2docker
gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/lib/node_modules/ember-cli/node_modules/bufferutil
gyp ERR! node -v v5.0.0
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok
/usr/lib
`-- bower#1.6.5
npm ERR! Linux 4.1.10-boot2docker
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "bower" "ember-cli"
npm ERR! node v5.0.0
npm ERR! npm v3.3.6
npm ERR! code ELIFECYCLE
npm ERR! bufferutil#1.1.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bufferutil#1.1.0 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the bufferutil package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls bufferutil
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /usr/src/app/npm-debug.log
You can try using:
npm install -g ember-cli --no-optional

Node-gyp build and rebuild error on centos

Getting below error when trying to build node-gyp build
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.35 | linux | x64 gyp info
spawn make gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory /root/node-latest-install/build'
CXX(target) Release/obj.target/binding/binding.o ../binding.cc:5:
error: ISO C++ forbids declaration of ‘FunctionCallbackInfo’ with no
type ../binding.cc:5: error: expected ‘,’ or ‘...’ before ‘<’ token
../binding.cc: In function ‘void Add(int)’: ../binding.cc:7: error: no
matching function for call to
‘v8::HandleScope::HandleScope(v8::Isolate
*&)’ /root/.node-gyp/0.10.35/deps/v8/include/v8.h:473: note: candidates are: v8::HandleScope::HandleSco
pe(const v8::HandleScope&)
/root/.node-gyp/0.10.35/deps/v8/include/v8.h:448: note:
v8::HandleScope::HandleSco pe()
../binding.cc:9: error: ‘args’ was not declared in this scope
../binding.cc:10: error: ‘class v8::Isolate’ has no member named
‘ThrowException’ ../binding.cc:11: error: ‘NewFromUtf8’ is not a
member of ‘v8::String’ ../binding.cc:15: error: ‘args’ was not
declared in this scope ../binding.cc:16: error: ‘class v8::Isolate’
has no member named ‘ThrowException’ ../binding.cc:17: error:
‘NewFromUtf8’ is not a member of ‘v8::String’ ../binding.cc:21: error:
‘args’ was not declared in this scope ../binding.cc:22: error: no
matching function for call to ‘v8::Number::New(v8::Isolate*&, double&)
’ /root/.node-gyp/0.10.35/deps/v8/include/v8.h:1381: note: candidates
are: static v8::Local<v8::Numb er>
v8::Number::New(double) ../binding.cc: In function ‘void
Init(v8::Handle<v8::Object>)’: ../binding.cc:28: error: no matching
function for call to ‘SetMethod(v8::Handle<v8::Object>&, cons
t char [4], void (&)(int))’ make: ***
[Release/obj.target/binding/binding.o] Error 1 make: Leaving directory
/root/node-latest-install/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:2
3) gyp ERR! stack at ChildProcess.emit (events.js:98:17) gyp ERR!
stack at Process.ChildProcess._handle.onexit
(child_process.js:820:12) gyp ERR! System Linux
2.6.32-504.3.3.el6.x86_64 gyp ERR! command "node" "/usr/local/bin/node-gyp" "build" gyp ERR! cwd
/root/node-latest-install gyp ERR! node -v v0.10.35 gyp ERR! node-gyp
-v v1.0.2 gyp ERR! not ok

Building NodeJs module with boost (or any other library for that matter)

I'm trying to build a nodejs module with C++ and Ubuntu 13.04 using some boost headers as shown below:
#include <iostream>
#include <string>
#include <node/node.h>
#include <v8.h>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace v8;
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
std::string foobar = "8";
return scope.Close(String::New("world"));
}
void init(Handle<Object> exports){
exports->Set(String::NewSymbol("Hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
However, when compiling with node-gyp, I get the following errors:
sam#ubuntu:~/workspace_cpp/NodeTest/src$ node-gyp build gyp info it
worked if it ends with ok gyp info using node-gyp#0.10.9 gyp info
using node#0.10.15 | linux | x64 gyp info spawn make gyp info spawn
args [ 'BUILDTYPE=Release', '-C', 'build' ] make: Entering directory
/home/sam/workspace_cpp/NodeTest/src/build' CXX(target)
Release/obj.target/hello/NodeTest.o In file included from
/usr/include/boost/numeric/conversion/converter.hpp:14:0,
from /usr/include/boost/numeric/conversion/cast.hpp:33,
from /usr/include/boost/lexical_cast.hpp:66,
from ../NodeTest.cpp:13: /usr/include/boost/numeric/conversion/converter_policies.hpp: In
member function ‘void
boost::numeric::def_overflow_handler::operator()(boost::numeric::range_check_result)’:
/usr/include/boost/numeric/conversion/converter_policies.hpp:162:31:
error: exception handling disabled, use -fexceptions to enable make:
*** [Release/obj.target/hello/NodeTest.o] Error 1 make: Leaving directory/home/sam/workspace_cpp/NodeTest/src/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.EventEmitter.emit (events.js:98:17) gyp ERR!
stack at Process.ChildProcess._handle.onexit
(child_process.js:789:12) gyp ERR! System Linux 3.8.0-19-generic gyp
ERR! command "node" "/usr/local/bin/node-gyp" "build" gyp ERR! cwd
/home/sam/workspace_cpp/NodeTest/src gyp ERR! node -v v0.10.15 gyp
ERR! node-gyp -v v0.10.9 gyp ERR! not ok
I cannot find anything on the web for how to get node-gyp to build with other libraries such as boost. Does anybody have any insight or experience with this? My ultimate goal is to make a SOAP module using gsoap.
EDIT
I'm assuming that I have to edit my binding.gyp file in some way to allow for boost to be compiled. As it sits, this file currently looks like this:
{ "targets": [
{
"target_name": "hello",
"sources": [ "NodeTest.cpp" ]
} ] }
For anybody else that has this error, the key is enabling exceptions for node-gyp. In your bindings.gyp file, make sure you include this
...
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ]
...
I found my solution in this forum post: GitHub