通过n-api在nodejs中使用多个cpp文件

use multiple cpp files in nodejs by n-api

本文关键字:cpp 文件 n-api nodejs 通过      更新时间:2023-10-16

我是n-api模块的新手。在组合两个cpp文件时,在执行节点gyp-configure build时得到以下错误。

b.obj : error LNK2005: _register_a_ already defined in a.obj [e:democppmorebuilda.vcxproj]
b.obj : error LNK2005: "struct napi_value__ * __cdecl Init(struct napi_env__ *,struct napi_value__ *
)" (?Init@@YAPEAUnapi_value__@@PEAUnapi_env__@@PEAU1@@Z) already defined in a.obj [e:democppmorebu
ilda.vcxproj]

我怀疑这是由于两个不同的cpp代码中存在napi_value Init((。如果是这样的话,我们该如何克服它&我们的gyp&js文件需要编写吗?

我有以下代码:

a.cc
-------
#include <node_api.h>
#include <assert.h>
napi_value Method(napi_env env, napi_callback_info info) {
//some code
}
#define DECLARE_NAPI_METHOD(name, func)                          
{ name, 0, func, 0, 0, 0, napi_default, 0 }
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
-----------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
binding.gyp
--------------
{
"targets": [
{
"target_name": "module",
"sources": [ 
"./src/a.cc",
"./src/b.cc" ]
}
}
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
#include <node_api.h>
#include <assert.h>
#include <stdio.h>
napi_value Add(napi_env env, napi_callback_info info) {
//some code here
}
#define DECLARE_NAPI_METHOD(name, func)                          
{ name, 0, func, 0, 0, 0, napi_default, 0 }
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor addDescriptor = DECLARE_NAPI_METHOD("add", Add);
status = napi_define_properties(env, exports, 1, &addDescriptor);
assert(status == napi_ok);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

我参加聚会迟到了。但由于这是唯一一个与我一直在努力解决的问题有关的帖子。幸运的是,我从官方nodejs中找到了这个例子(你也可以找到其他例子(,所以我想在这里分享它:https://github.com/nodejs/node-addon-examples/tree/master/6_object_wrap

编辑:我包括了我实现这个问题的方式:

Init方法中,我将descriptor声明为一个数组。napi_define_properties允许您传入一个方法数组。

代码:

// addon.cc
#include <node_api.h>
static napi_value Add()
{
// Code
}
static napi_value Subtract()
{
// Code
}
#define DECLARE_NAPI_METHOD(name, func) { name, 0, func, 0, 0, 0, napi_default, 0 }
static napi_value Init(napi_env env, napi_value exports)
{
napi_status status;
// Declare descriptor as an array
napi_property_descriptor desc[] = { 
DECLARE_NAPI_METHOD("subtract", Subtract),
DECLARE_NAPI_METHOD("add", Add)
};
// Remeber to change the length of the descriptor as well
status = napi_define_properties(env, exports, sizeof(desc) / sizeof(*desc), desc);
assert(status == napi_ok);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)