通过n-api在nodejs中使用多个cpp文件
use multiple cpp files in nodejs by n-api
我是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)
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 包含在.cpp文件中包含在 .h 文件时包含
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 如何使用线程类编译 cpp 文件?
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 我应该将外部标头放在 .h 文件还是.cpp文件中?
- 从另一个 cpp 文件更改结构内、映射键内的变量