编译nw-node版本的插件
下载nwjs对应版本的nodejs
debug版nwjs工具: https://dl.nwjs.io/v0.60.0/nwjs-sdk-v0.60.0-linux-x64.tar.gz
release版nwjs工具: https://dl.nwjs.io/v0.60.0/nwjs-v0.60.0-linux-x64.tar.gz
nwjs对应版本的nodejs: https://cdn.npmmirror.com/binaries/node/latest-v17.x/node-v17.3.0-linux-x64.tar.gz
测试源码
目录结构
yh@yh:~/mymath$ tree
.
├── addon.cc //插件接口目录
├── binding.gyp //编译配置文件
├── CppLib //c++源码文件
│ ├── mynumber.cpp
│ └── mynumber.h
├── myobject.cc //nodejs接口类
├── myobject.h
├── package.json //项目配置文件
└── test.js //javascript接口测试文件
1 directory, 8 files
配置环境
export PATH=$HOME/node/bin:$HOME/node/lib/node_modules/npm/bin/node-gyp-bin:$PATH
sudo apt install g++ gcc make python patchelf tree
FQ然后开始配置编译环境
下载nw的插件编译工具
npm install -g nw-gyp
编译所使用的c++库
cd CppLib && g++ -shared -fPIC -o libmynumber.so mynumber.cpp && mv libmynumber.so .. && cd ..
编译NW0.60.0版本的node插件
nw-gyp configure --target=0.60.0
nw-gyp rebuild --target=0.60.0
mv build/Release/mymath.node .
修改编译出的addon.node文件中所依赖的c++库 libmynumber.so的路径
patchelf --replace-needed $PWD/libmynumber.so libmynumber.so mymath.node
patchelf --set-rpath "\$ORIGIN" mymath.node
运行ldd检查插件的库依赖
ldd mymath.node
linux-vdso.so.1 (0x00007fffac9c8000)
libmynumber.so => /home/yh/addon/./libmynumber.so (0x00007fbfd6623000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fbfd6432000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fbfd640f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbfd621d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbfd60ce000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbfd6633000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fbfd60b3000)
将mymath.node、libnumber.so和test.js复制到nw的环境中测试
在nw坏境下加上以下两个文件
index.html
DOCTYPE html>
<html>
<head>
<title>Hello World!title>
head>
<body>
<h1>Hello World!h1>
<script src=test.js>script>
body>
html>
package.json
{
"name": "测试",
"version": "0.1",
"main": "index.html",
"chromium-args":"--mixed-context",
"webkit": {
"page-cache": false
},
"window": {
"show": true,
"toolbar": true,
"frame": true,
"position": "center",
"icon": "logo.png",
"width": 600,
"height": 400,
"min_width": 600,
"min_height": 400
},
"dependencies": {
"nw-gyp": "^3.6.6"
}
}
./nw index.html
10 + 1 = 11
11 + 1 = 12
12 + 1 = 13
20 + 1 = 21
21 + 1 = 22
22 + 1 = 23
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2.5
其他
编译nodejs版本插件
node-gyp configure
node-gyp rebuild
用node测试addon.node模块
node ./test.js
10 + 1 = 11
11 + 1 = 12
12 + 1 = 13
20 + 1 = 21
21 + 1 = 22
22 + 1 = 23
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2.5