0

我是 n-api 模块的新手。在组合两个 cpp 文件时,在执行 node-gyp 配置构建时出现以下错误。

b.obj : error LNK2005: _register_a_ already defined in a.obj [e:\democppmore\build\a.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:\democppmore\bu
ild\a.vcxproj]

我怀疑这是由于 napi_value Init() 存在于 2 个不同的 cpp 代码中。如果是这样,我们如何克服它以及我们的 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)

4

1 回答 1

0

我参加晚会迟到了。但由于这是唯一与我正在努力解决的问题相关的帖子。幸运的是,我从官方找到了示例(您也可以找到其他示例)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)
于 2021-02-04T05:22:35.343 回答