所以,我尝试在我的 node-webkit(nwjs) 应用程序中使用 ffi,但我的代码不起作用。有我的 index.html 文件:
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<script>
var ffi = require('ffi');
var ref = require('ref');
var int = ref.types.int;
var platform = process.platform;
var mathlibLoc = null;
if (platform === 'win32') {
mathlibLoc = './math.dll';
} else if (platform === 'linux') {
mathlibLoc = './math.so';
} else if (platform === 'darwin') {
mathlibLoc = './math.dylib'
} else {
throw new Error('unsupported plateform for mathlibLoc')
}
var math = ffi.Library(mathlibLoc, {
"add": [int, [int, int]],
"minus": [int, [int, int]],
"multiply": [int, [int, int]]
});
var result = null;
result = math.add(5, 2);
console.log('5+2=' + result);
result = math.minus(5, 2);
console.log('5-2=' + result);
result = math.multiply(5, 2);
console.log('5*2=' + result);
</script>
</body>
</html>
很有趣的是我的脚本不起作用然后我尝试写这一行
var ffi = 需要('ffi');
而且无论我尝试在这条线之后运行什么,它都不起作用......
所以,在我翻遍了所有的互联网之后,我发现我需要以某种方式使用 nw-gyp。首先,我在我的项目文件夹中运行这个命令
npm 安装 ffi
然后我转到文件夹 ./node_modules/ffi 并尝试运行此命令:
nw-gyp 重建 --target=0.30.0 --arch=x64
我在控制台中有什么:
gyp info it worked if it ends with ok
gyp info using nw-gyp@3.6.3
gyp info using node@8.11.1 | win32 | x64
gyp info spawn D:\Python27\python.EXE
gyp info spawn args [ 'C:\\Users\\dchar\\AppData\\Roaming\\npm\\node_modules\\nw-gyp\\gyp\\gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'msvs',
gyp info spawn args '-G',
gyp info spawn args 'msvs_version=auto',
gyp info spawn args '-I',
gyp info spawn args 'C:\\Users\\dchar\\Desktop\\nwjsdll\\node_modules\\ffi\\build\\config.gypi',
gyp info spawn args '-I',
gyp info spawn args 'C:\\Users\\dchar\\AppData\\Roaming\\npm\\node_modules\\nw-gyp\\addon.gypi',
gyp info spawn args '-I',
gyp info spawn args 'C:\\Users\\dchar\\.nw-gyp\\0.30.0\\common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=C:\\Users\\dchar\\.nw-gyp\\0.30.0',
gyp info spawn args '-Dnode_gyp_dir=C:\\Users\\dchar\\AppData\\Roaming\\npm\\node_modules\\nw-gyp',
gyp info spawn args '-Dnode_lib_file="C:\\Users\\dchar\\.nw-gyp\\0.30.0\\<(target_arch)\\node.lib"',
gyp info spawn args '-Dnw_lib_file="C:\\Users\\dchar\\.nw-gyp\\0.30.0\\<(target_arch)\\nw.lib"',
gyp info spawn args '-Dmodule_root_dir=C:\\Users\\dchar\\Desktop\\nwjsdll\\node_modules\\ffi',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '-Dv13=1',
gyp info spawn args '--generator-output',
gyp info spawn args 'C:\\Users\\dchar\\Desktop\\nwjsdll\\node_modules\\ffi\\build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info spawn C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args '/clp:Verbosity=minimal',
gyp info spawn args '/nologo',
gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
Building assembly file ..\..\..\deps\libffi\src\x86\win64.asm
Assembling: ..\..\..\deps\libffi\src\x86\win64.asm
prep_cif.c
types.c
raw_api.c
java_raw_api.c
closures.c
ffi.c
win_delay_load_hook.cc
ffi.vcxproj -> C:\Users\dchar\Desktop\nwjsdll\node_modules\ffi\build\Release\\libffi.lib
ffi.cc
callback_info.cc
threaded_callback_invokation.cc
win32-dlfcn.cc
win_delay_load_hook.cc
..\src\ffi.cc(111): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\ffi.cc(114): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\ffi.cc(123): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\ffi.cc(124): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\ffi.cc(125): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\ffi.cc(126): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\ffi.cc(132): error C2039: 'ForceSet': is not a member of 'v8::Object' [C:\Users\dchar\Desktop\nwjsdll\node_modul
es\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\.nw-gyp\0.30.0\deps\v8\include\v8.h(3103): note: see declaration of 'v8::Object'
..\src\callback_info.cc(63): warning C4996: 'Nan::Callback::Call': was declared deprecated [C:\Users\dchar\Desktop\nwjs
dll\node_modules\ffi\build\ffi_bindings.vcxproj]
..\src\ffi.cc(58): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'C:\U
sers\dchar\Desktop\nwjsdll\node_modules\nan\nan.h(1618): note: see declaration of 'Nan::Callback::Call'
..\src\ffi.cc(59): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
..\src\callback_info.cc(73): warning C4996: 'Nan::Callback::Call': was declared deprecated [C:\Users\dchar\Desktop\nwjs
dll\node_modules\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
C:\Users\dchar\Desktop\nwjsdll\node_modules\nan\nan.h(1618): note: see declaration of 'Nan::Callback::Call'
..\src\ffi.cc(60): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
..\src\callback_info.cc(78): warning C4996: 'Nan::Callback::Call': was declared deprecated [C:\Users\dchar\Desktop\nwjs
dll\node_modules\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'C:\U
sers\dchar\Desktop\nwjsdll\node_modules\nan\nan.h(1618): note: see declaration of 'Nan::Callback::Call'
..\src\ffi.cc(63): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(64): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(65): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(78): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(88): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(91): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(94): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(97): warning C4996: 'Nan::ForceSet': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_modules
\ffi\build\ffi_bindings.vcxproj]
c:\users\dchar\desktop\nwjsdll\node_modules\nan\nan_maybe_43_inl.h(130): note: see declaration of 'Nan::ForceSet'
..\src\ffi.cc(367): warning C4996: 'Nan::Callback::Call': was declared deprecated [C:\Users\dchar\Desktop\nwjsdll\node_
modules\ffi\build\ffi_bindings.vcxproj]
C:\Users\dchar\Desktop\nwjsdll\node_modules\nan\nan.h(1618): note: see declaration of 'Nan::Callback::Call'
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Users\dchar\AppData\Roaming\npm\node_modules\nw-gyp\lib\build.js:261:23)
gyp ERR! stack at emitTwo (events.js:126:13)
gyp ERR! stack at ChildProcess.emit (events.js:214:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
gyp ERR! System Windows_NT 10.0.16299
gyp ERR! command "D:\\nodejs\\node.exe" "C:\\Users\\dchar\\AppData\\Roaming\\npm\\node_modules\\nw-gyp\\bin\\nw-gyp.js" "rebuild" "--target=0.30.0" "--arch=x64"
gyp ERR! cwd C:\Users\dchar\Desktop\nwjsdll\node_modules\ffi
gyp ERR! node -v v8.11.1
gyp ERR! nw-gyp -v v3.6.3
gyp ERR! not ok
请帮帮我!