在 centos 6.8 上编译 nodejs v4

Compiling for nodejs v4 on centos 6.8

我在安装需要在 centos 6.8 上编译的 nodejs 库时遇到问题。看起来应该使用较新版本的 gcc-c++ 编译节点 v4 依赖项。 我按照许多帖子中的建议安装了 devtoolset v3,以便能够使用 gcc-4.9。但它仍然失败并显示相同的错误消息,提示我仍在使用不支持 c++11 的编译器。 我做了 strace,看起来 devtools 没有在安装过程期望找到它的地方提供 libgcc,所以它最终采用了常规的 gcc-4.4

设置:

[vagrant@localhost sflow]$ cat /etc/centos-release 
CentOS release 6.8 (Final)
[vagrant@localhost sflow]$ node -v
v4.4.7
[vagrant@localhost sflow]$ npm -v
2.15.8

没有开发工具集:

[vagrant@localhost sflow]$ g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 

npm 安装

[vagrant@localhost sflow]$ npm instal --save pcap2
npm WARN package.json sflow@1.0.0 No description
npm WARN package.json sflow@1.0.0 No repository field.
npm WARN package.json sflow@1.0.0 No README data
\
> socketwatcher@0.3.0 install /home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher
> node-gyp rebuild

make: Entering directory `/home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher/build'
  CXX(target) Release/obj.target/socketwatcher/socket_watcher.o
In file included from ../socket_watcher.hpp:8,
                 from ../socket_watcher.cpp:5:
../../nan/nan.h:43:3: error: #error This version of node/NAN/v8 requires a C++11 compiler
In file included from /home/vagrant/.node-gyp/4.4.7/include/node/node.h:42,
                 from ../../nan/nan.h:47,
                 from ../socket_watcher.hpp:8,
                 from ../socket_watcher.cpp:5:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:336: error: expected unqualified-id before ‘using’
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In constructor ‘v8::MaybeLocal<T>::MaybeLocal()’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:353: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘bool v8::MaybeLocal<T>::IsEmpty() const’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:360: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘bool v8::MaybeLocal<T>::ToLocal(v8::Local<S>*) const’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:364: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘bool v8::WeakCallbackInfo<T>::IsFirstPass() const’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:430: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: At global scope:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:469: error: expected unqualified-id before ‘using’
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In constructor ‘v8::Global<T>::Global()’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:790: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In constructor ‘v8::Global<T>::Global(v8::Global<T>&&)’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:815: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘v8::Global<T>& v8::Global<T>::operator=(v8::Global<S>&&)’:
... skipped ...
../../nan/nan.h:2256: error: expected unqualified-id before ‘>’ token
../../nan/nan.h:2265: error: expected constructor, destructor, or type conversion before ‘<’ token
../socket_watcher.cpp: In static member function ‘static void SocketWatcher::Initialize(v8::Local<v8::Object>)’:
../socket_watcher.cpp:22: error: ‘class v8::Local<v8::Boolean>’ has no member named ‘ToLocalChecked’
../socket_watcher.cpp:30: error: ‘class v8::Local<v8::Boolean>’ has no member named ‘ToLocalChecked’
../socket_watcher.cpp: In static member function ‘static void SocketWatcher::Callback(uv_poll_t*, int, int)’:
../socket_watcher.cpp:62: error: ‘class v8::Local<v8::Boolean>’ has no member named ‘ToLocalChecked’
../socket_watcher.cpp:63: error: ‘Get’ is not a member of ‘Nan’
make: *** [Release/obj.target/socketwatcher/socket_watcher.o] Error 1
make: Leaving directory `/home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher/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:276: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 2.6.32-642.el6.x86_64
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher
gyp ERR! node -v v4.4.7
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok 
npm ERR! Linux 2.6.32-642.el6.x86_64
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "instal" "--save" "pcap2"
npm ERR! node v4.4.7
npm ERR! npm  v2.15.8
npm ERR! code ELIFECYCLE

npm ERR! socketwatcher@0.3.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the socketwatcher@0.3.0 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the socketwatcher 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 information on how to open an issue for this project with:
npm ERR!     npm bugs socketwatcher
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls socketwatcher
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/vagrant/sflow/npm-debug.log

开发工具集:

[vagrant@localhost sflow]$ scl enable python27 devtoolset-3 bash
[vagrant@localhost sflow]$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-3/root/usr/libexec/gcc/x86_64-redhat-linux/4.9.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-3/root/usr --mandir=/opt/rh/devtoolset-3/root/usr/share/man --infodir=/opt/rh/devtoolset-3/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,fortran,lto --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/cloog-install --with-mpc=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/mpc-install --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) 

npm 安装

[vagrant@localhost sflow]$ npm install --save pcap2
npm WARN package.json sflow@1.0.0 No description
npm WARN package.json sflow@1.0.0 No repository field.
npm WARN package.json sflow@1.0.0 No README data
-
> socketwatcher@0.3.0 install /home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher
> node-gyp rebuild

make: Entering directory `/home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher/build'
  CXX(target) Release/obj.target/socketwatcher/socket_watcher.o
In file included from ../socket_watcher.hpp:8,
                 from ../socket_watcher.cpp:5:
../../nan/nan.h:43:3: error: #error This version of node/NAN/v8 requires a C++11 compiler
In file included from /home/vagrant/.node-gyp/4.4.7/include/node/node.h:42,
                 from ../../nan/nan.h:47,
                 from ../socket_watcher.hpp:8,
                 from ../socket_watcher.cpp:5:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:336: error: expected unqualified-id before ‘using’
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In constructor ‘v8::MaybeLocal<T>::MaybeLocal()’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:353: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘bool v8::MaybeLocal<T>::IsEmpty() const’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:360: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘bool v8::MaybeLocal<T>::ToLocal(v8::Local<S>*) const’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:364: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘bool v8::WeakCallbackInfo<T>::IsFirstPass() const’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:430: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: At global scope:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:469: error: expected unqualified-id before ‘using’
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In constructor ‘v8::Global<T>::Global()’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:790: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In constructor ‘v8::Global<T>::Global(v8::Global<T>&&)’:
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h:815: error: ‘nullptr’ was not declared in this scope
/home/vagrant/.node-gyp/4.4.7/include/node/v8.h: In member function ‘v8::Global<T>& v8::Global<T>::operator=(v8::Global<S>&&)’:
... skipped ...
../../nan/nan.h:2256: error: ‘MaybeLocal’ was not declared in this scope
../../nan/nan.h:2256: error: template argument 1 is invalid
../../nan/nan.h:2256: error: expected unqualified-id before ‘>’ token
../../nan/nan.h:2265: error: expected constructor, destructor, or type conversion before ‘<’ token
../socket_watcher.cpp: In static member function ‘static void SocketWatcher::Initialize(v8::Local<v8::Object>)’:
../socket_watcher.cpp:22: error: ‘class v8::Local<v8::Boolean>’ has no member named ‘ToLocalChecked’
../socket_watcher.cpp:30: error: ‘class v8::Local<v8::Boolean>’ has no member named ‘ToLocalChecked’
../socket_watcher.cpp: In static member function ‘static void SocketWatcher::Callback(uv_poll_t*, int, int)’:
../socket_watcher.cpp:62: error: ‘class v8::Local<v8::Boolean>’ has no member named ‘ToLocalChecked’
../socket_watcher.cpp:63: error: ‘Get’ is not a member of ‘Nan’
make: *** [Release/obj.target/socketwatcher/socket_watcher.o] Error 1
make: Leaving directory `/home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher/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:276: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 2.6.32-642.el6.x86_64
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/vagrant/sflow/node_modules/pcap2/node_modules/socketwatcher
gyp ERR! node -v v4.4.7
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok 
npm ERR! Linux 2.6.32-642.el6.x86_64
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "--save" "pcap2"
npm ERR! node v4.4.7
npm ERR! npm  v2.15.8
npm ERR! code ELIFECYCLE

npm ERR! socketwatcher@0.3.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the socketwatcher@0.3.0 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the socketwatcher 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 information on how to open an issue for this project with:
npm ERR!     npm bugs socketwatcher
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls socketwatcher
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/vagrant/sflow/npm-debug.log

跟踪:

[vagrant@localhost sflow]$ strace npm install --save pcap2 > strace.log 2>&1
[vagrant@localhost sflow]$ grep gcc strace.log 
open("/opt/rh/devtoolset-3/root/usr/lib64/tls/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-3/root/usr/lib64/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-3/root/usr/lib/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/opt/rh/python27/root/usr/lib64/tls/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/opt/rh/python27/root/usr/lib64/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/libgcc_s.so.1", O_RDONLY)  = 3

node-gyp 重建

[vagrant@localhost sflow]$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-3/root/usr/libexec/gcc/x86_64-redhat-linux/4.9.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-3/root/usr --mandir=/opt/rh/devtoolset-3/root/usr/share/man --infodir=/opt/rh/devtoolset-3/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,fortran,lto --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/cloog-install --with-mpc=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/mpc-install --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) 
[vagrant@localhost sflow]$ env CXX="g++ -v" node-gyp rebuild
gyp info it worked if it ends with ok
gyp info using node-gyp@3.4.0
gyp info using node@4.4.7 | linux | x64
gyp info spawn /opt/rh/python27/root/usr/bin/python2
gyp info spawn args [ '/usr/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   '/home/vagrant/sflow/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/usr/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/home/vagrant/.node-gyp/4.4.7/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/home/vagrant/.node-gyp/4.4.7',
gyp info spawn args   '-Dnode_gyp_dir=/usr/lib/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=node.lib',
gyp info spawn args   '-Dmodule_root_dir=/home/vagrant/sflow',
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: binding.gyp not found (cwd: /home/vagrant/sflow) while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/lib/node_modules/node-gyp/lib/configure.js:305:16)
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 2.6.32-642.el6.x86_64
gyp ERR! command "/usr/bin/node" "/usr/bin/node-gyp" "rebuild"
gyp ERR! cwd /home/vagrant/sflow
gyp ERR! node -v v4.4.7
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok 

所以,问题是 - 我在应用 devtoolset 以在 centos6 上使用 gcc-4.9 时缺少什么? 或者有没有其他方法在 centos6 上连接 v4 和 pcap2?

需要兼容 C++11 的编译器(例如 gcc 4.8 或更高版本)。在 post 末尾的 node-gyp rebuild 输出中,您的 4.9.2 安装看起来仍然没有被选中。您可以使用 env CC=/path/to/gcc CXX=/path/to/g++ LINKXX=/path/to/g++ LINK=/path/to/gcc node-gyp rebuild.

设置指向正确 gcc/g++ 二进制文件的显式路径

(原为评论,现post编辑为答案。)