gpt4 book ai didi

c++ - 可以构建自定义 node.js 插件但不能包含

转载 作者:IT老高 更新时间:2023-10-28 23:17:42 25 4
gpt4 key购买 nike

我正在编写一个使用 C++ 与 C 程序混合的自定义 node.js 插件。

addon.cc 包含类似

#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <node_buffer.h>

using namespace v8;
using namespace node;


/* other logic and function... */


Handle<Value> RunCallback(const Arguments& args) {
HandleScope scope;

Local<Value> buffer1 = args[0];
size_t size = Buffer::Length(buffer1->ToObject());
char* bufferdata = Buffer::Data(buffer1->ToObject());

/* some logic written in C style ... */

Local<Function> cb = Local<Function>::Cast(args[1]);
const unsigned argc = 1;
Local<Value> argv[argc] = { Local<Value>::New(String::New(outputdata, outputSize)) };

cb->Call(Context::GetCurrent()->Global(), argc, argv);

return scope.Close(Undefined());
}

void Init(Handle<Object> target) {
target->Set(String::NewSymbol("runCallback"), FunctionTemplate::New(RunCallback)->GetFunction());
}

NODE_MODULE(addon, Init)

它还包括其他.cc文件,所以wscript是这样的:

srcdir = '.'
blddir = 'build'
VERSION = '0.0.1'

def set_options(opt):
opt.tool_options('compiler_cxx')

def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')

def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.target = 'addon'
obj.source = ['addon.cc', 'otherFiles.cc']

当我运行 node-waf configure 时,它​​显示:

Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp : /usr/bin/cpp
Checking for program ar : /usr/bin/ar
Checking for program ranlib : /usr/bin/ranlib
Checking for g++ : ok
Checking for node path : not found
Checking for node prefix : ok /usr/local
'configure' finished successfully (0.169s)

当我运行 node-waf build 时,它显示:

Waf: Entering directory `/path/build'
[ 1/25] cxx: addon.cc -> build/Release/addon_1.o
... list of file ...
build/Release/list of file -> build/Release/addon.node
Waf: Leaving directory `/path/build'
'build' finished successfully (0.544s)

但是当我在 Node REPL 中尝试以下操作时,它显示:

var addon = require("./build/Release/addon");
Error: Unable to load shared library /path/build/Release/addon.node
at Object..node (module.js:472:11)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at repl:1:13
at REPLServer.eval (repl.js:80:21)
at repl.js:190:20
at REPLServer.eval (repl.js:87:5)
at Interface.<anonymous> (repl.js:182:12)

这很奇怪。我已检查该文件是否应与系统架构匹配:

$ file build/Release/addon.node 
build/Release/addon.node: Mach-O 64-bit bundle x86_64

$ file `which node`
/usr/local/bin/node: Mach-O 64-bit executable x86_64

通过查看 nm,它显示以下内容:

nm ./build/Release/addon.node
0000000000011880 s GCC_except_table30
0000000000001160 t _Init
U __Unwind_Resume_or_Rethrow
U ___bzero
U ___gxx_personality_v0
0000000000013220 D _addon_module
U _free
0000000000013600 D _lsfmeanTbl
0000000000013420 D _memLfTbl
U _memcpy
U _memmove
U _puts
U _realloc
0000000000011950 s _ssqEn_win.2272
000000000001341c D _stMemLTbl
00000000000132e0 D _state_frgqTbl
00000000000132c0 D _state_sq3Tbl
U dyld_stub_binder
(... many are omitted ...)

可能的原因是什么?是不是我不能只将 C 文件与 C++ 文件结合起来进行编译?我应该删除所有 malloc/realloc/free 吗?还是有其他可能的原因?

最佳答案

将 Init() 的定义和 NODE_MODULE 宏包装在 extern "C" 中以避免导出符号的名称 C++ 错误:

extern "C" {

void Init(Handle<Object> target) {
target->Set(String::NewSymbol("runCallback"), FunctionTemplate::New(RunCallback)->GetFunction());
}

NODE_MODULE(addon, Init)
}

关于c++ - 可以构建自定义 node.js 插件但不能包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com