簡體   English   中英

通過n-api在nodejs中使用多個cpp文件

[英]use multiple cpp files in nodejs by n-api

我是 n-api 模塊的新手。 在組合兩個 cpp 文件時,在執行 node-gyp configure build 時出現以下錯誤。

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]

我懷疑這是由於存在於 2 個不同的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM