gpt4 book ai didi

javascript - node.js c++ 将多个cc文件扩展到一个模块中

转载 作者:太空狗 更新时间:2023-10-29 23:04:28 24 4
gpt4 key购买 nike

我正在尝试使用 node.js 来评估一个复杂的基于 c 的库如何与 node.js 连接。该库对数据库执行多项操作,nodejs 应调用该函数并通过 rest api 返回结果。

在使用 node.js 中的示例时一边,我遇到了一个问题:

我正在尝试基于两个 cc 文件构建一个插件:

插件1.c:

#define BUILDING_NODE_EXTENSION
#include <node.h>

using namespace v8;

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

if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}

if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}

Local<Number> num = Number::New(args[0]->NumberValue() +
args[1]->NumberValue());
return scope.Close(num);
}

void Init(Handle<Object> exports) {
exports->Set(String::NewSymbol("add"),
FunctionTemplate::New(Add)->GetFunction());
}

NODE_MODULE(addon, Init)

插件2.c:

#define BUILDING_NODE_EXTENSION
#include <node.h>

using namespace v8;

Handle<Value> Del(const Arguments& args) {
HandleScope scope2;

if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope2.Close(Undefined());
}

if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope2.Close(Undefined());
}

Local<Number> num = Number::New(args[0]->NumberValue() -
args[1]->NumberValue());
return scope2.Close(num);
}

void Init(Handle<Object> exports) {
exports->Set(String::NewSymbol("del"),
FunctionTemplate::New(Del)->GetFunction());
}

NODE_MODULE(addon, Init)

还有我的 binding.gyp:

{
"targets": [
{
"target_name": "addon",
"sources": ["addon1.cc", "addon2.cc"]
}
]
}

调用 node.gyp 配置构建以以下错误消息结束:

  CXX(target) Release/obj.target/addon/addon2.o
SOLINK_MODULE(target) Release/addon.node
duplicate symbol __Z4InitN2v86HandleINS_6ObjectEEE in:
Release/obj.target/addon/addon1.o
Release/obj.target/addon/addon2.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Release/addon.node] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/opt/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:797:12)
gyp ERR! System Darwin 13.1.0
gyp ERR! command "node" "/opt/local/bin/node-gyp" "configure" "build"
gyp ERR! cwd /Applications/MAMP/htdocs/nodejs/test2
gyp ERR! node -v v0.10.26
gyp ERR! node-gyp -v v0.13.0
gyp ERR! not ok

我希望你们中的任何人都可以提示如何解决这个问题。

提前致谢。

最佳答案

问题很清楚:你有一个名为 Init 的函数在两个翻译单元(源文件)中。当您构建单个 模块时,您应该只有一个 Init功能。

解决这个问题的简单方法是删除一个 Init功能,并“注册”两者adddel其余的名字Init功能。


解决方案:在一个 源文件中,您只有 Init函数,并声明缺失的AddDel函数(即你制作函数原型(prototype)):

// Declare function prototypes
Handle<Value> Add(const Arguments& args);
Handle<Value> Del(const Arguments& args);

// The one and only initialization function
void Init(Handle<Object> exports) {
exports->Set(String::NewSymbol("add"),
FunctionTemplate::New(Add)->GetFunction());
exports->Set(String::NewSymbol("del"),
FunctionTemplate::New(Del)->GetFunction());
}

NODE_MODULE(addon, Init)

实际上,您现在可以将其作为单独的源文件。

关于javascript - node.js c++ 将多个cc文件扩展到一个模块中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22833632/

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