c++ - 如何在 Node.js 的 C++ 插件中使用 std::string?

标签 c++ node.js std v8 node.js-addon

我正在尝试按照此处的 node.js 文档包装 C++ 对象以在 javascript 中使用:https://nodejs.org/api/addons.html#addons_wrapping_c_objects

该插件将正确构建,并且当我的对象“AnObject”仅具有数字属性(即“int32_tage;”)时可以正常工作。

当我添加属性“std::string name;”时对于 AnObject,“node-gyp configure”有效,但是“node-gyp build”给出了以下错误:

Wills-MacBook-Pro:cppObjectWrapping willalley$ node-gyp build
[...]
In file included from ../personAddon.cc:3:
../AnObject.h:24:21: error: implicit instantiation of undefined template 
'std::basic_string<char, std::char_traits<char>,
      std::allocator<char> >'
        std::string name;
                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
/MacOSX10.12.sdk/usr/include/c++/4.2.1/bits/stringfwd.h:56:11: note: 
      template is declared here
    class basic_string;
          ^
1 error generated.
[...]
Wills-MacBook-Pro:cppObjectWrapping willalley$

这是我的对象头文件,其中抛出错误,AnObject.h:

    #pragma once

    #include <node.h>
    #include <node_object_wrap.h>
    #include <string.h>

    namespace demo {

        class AnObject : public node::ObjectWrap {
        public:
            static void Init(v8::Local<v8::Object> exports);

        private:
            explicit AnObject(int32_t num = 19);
            ~AnObject();

            static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
            static void setAge(const v8::FunctionCallbackInfo<v8::Value>& args);
            static void getAge(const v8::FunctionCallbackInfo<v8::Value>& args);
            static void setName(const v8::FunctionCallbackInfo<v8::Value>& args);
            static void getName(const v8::FunctionCallbackInfo<v8::Value>& args);
            static v8::Persistent<v8::Function> constructor;
            int32_t age;
            std::string name;
        };

    }  // namespace demo

如果您好奇的话,可以查看实现文件 AnObject.cc:

#include "AnObject.h"
#include <string.h>

namespace demo {

    using v8::Context;
    using v8::Function;
    using v8::FunctionCallbackInfo;
    using v8::FunctionTemplate;
    using v8::Isolate;
    using v8::Local;
    using v8::Number;
    using v8::Object;
    using v8::Persistent;
    using v8::String;
    using v8::Value;

    Persistent<Function> AnObject::constructor;

    AnObject::AnObject(int32_t age_) : age(age_) {
    }

    AnObject::~AnObject() {
    }

    void AnObject::Init(Local<Object> exports) {
        Isolate* isolate = exports->GetIsolate();

        // Prepare constructor template
        Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
        tpl->SetClassName(String::NewFromUtf8(isolate, "AnObject"));
        tpl->InstanceTemplate()->SetInternalFieldCount(1);

        // Prototype
        NODE_SET_PROTOTYPE_METHOD(tpl, "setAge", setAge);
        NODE_SET_PROTOTYPE_METHOD(tpl, "setName", setName);
        NODE_SET_PROTOTYPE_METHOD(tpl, "getAge", getAge);
        NODE_SET_PROTOTYPE_METHOD(tpl, "getName", getName);

        constructor.Reset(isolate, tpl->GetFunction());
        exports->Set(String::NewFromUtf8(isolate, "AnObject"),
                     tpl->GetFunction());
    }

    void AnObject::New(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();

        if (args.IsConstructCall()) {
            // Invoked as constructor: `new AnObject(...)`
            int32_t value = args[0]->IsUndefined() ? 0 : args[0]->Int32Value();;
            AnObject* obj = new AnObject(value);
            obj->Wrap(args.This());
            args.GetReturnValue().Set(args.This());
        } else {
            // Invoked as plain function `AnObject(...)`, turn into construct call.
            const int argc = 1;
            Local<Value> argv[argc] = { args[0] };
            Local<Context> context = isolate->GetCurrentContext();
            Local<Function> cons = Local<Function>::New(isolate, constructor);
            Local<Object> result =
            cons->NewInstance(context, argc, argv).ToLocalChecked();
            args.GetReturnValue().Set(result);
        }
    }

    void AnObject::setAge(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
        if (args[0]->IsInt32()){
            obj->age = args[0]->Int32Value();
            args.GetReturnValue().Set(Number::New(isolate, obj->age));
        }
    }

    void AnObject::setName(const v8::FunctionCallbackInfo<v8::Value>& args) {
        Isolate* isolate = args.GetIsolate();
        AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());

        if (args[0]->isString()) {
            std::string input(*v8::String::Utf8Value(args[0]->ToString()));
            obj->name = input;
            args.GetReturnValue().Set(Number::New(isolate, obj->name));
        }
    }

    void AnObject::getAge(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
        args.GetReturnValue().Set(Number::New(isolate, obj->age));
    }

    void AnObject::getName(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        AnObject* obj = ObjectWrap::Unwrap<AnObject>(args.Holder());
        v8::Local<v8::String> output = v8::String::NewFromUtf8(isolate, name.cstr());
        args.GetReturnValue().Set(Number::New(isolate, output));
    }

}  // namespace demo

最佳答案

我发现的值得一试的解决方案是:

更改:

#include <string.h>

至:

#include <string>

在 AnObject.h 和 AnObject.cc 中

关于c++ - 如何在 Node.js 的 C++ 插件中使用 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539693/

相关文章:

c++ - Python 属性和 Swig

c++ - regex_replace 在 Windows 上不起作用

javascript - 在 Node JS 服务器端加载预编译的 Handlebar 模板

javascript - 为什么我得到 ENONET : no such file or directory when the file exists? node.js

c++ - std::pair 内的初始化列表

c++ - 标准字符串查找方法和计数算法的逻辑错误

c++ - 将许多值映射到 boost::unordered_map 中的单个键?

c++ - 在 C++ 中获取 JPEG 的尺寸

c++ - 在一个应用程序中创建两个窗口?

java - 无法在 CentOS 7 上安装 JHipster