【nodejs】node.js c++插件使用protobuf

基于【c++】c++项目使用protobuf,以下记录的是nodejs的c++插件使用protobuf遇到的问题。

区别就是设置头文件、库名、库路径的方式,前者是在vs项目属性设置,后者需要写binding.gyp(参考 gyp 文件输入格式参考

 

binding.gyp

  • include_dirs   
    • 头文件 相当于项目属性配置-c/c++-常规-附加包含目录
  • libraries 

    • lib库名 相当于项目属性配置-链接器-常规-附加库目录

  • library_dirs

    • lib库所在路径 相当于项目属性配置-链接器-输入-附加依赖项

 

报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xb6 in position 247: illegal multibyte sequence

解决:binding.gyp里写了中文注释,binding.gyp文件的编码格式要从原来的utf-8改为GBK

 

报错 error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”

参考 error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2

原因:c++插件生成的是release版本,用的lib库是debug版本

重新生成relase版本的libprotobuf.lib,libprotoc.lib

(注意debug版本的lib名最后多一个d的,所以要稍微改一下binding.gyp的libraries项)

使用命令 node-gyp rebuild 重新生成项目

 

代码

文件结构

binding.gyp

{
    "targets": [
        {
            "target_name": "test",
            "sources": ["src/test.cpp", "Account.pb.cc"],
            "include_dirs": [
                "<!(node -e \"require('nan')\")",
                "include",  # 头文件 相当于项目属性配置-c/c++-常规-附加包含目录
            ],

            'link_settings': {
                'libraries': [
                    "libprotobuf.lib",  # lib库名 相当于项目属性配置-链接器-常规-附加库目录
                    "libprotoc.lib",
                ],
                'library_dirs': [
                    'E:\\electronTest\\backTest\\static_libs\\Release', # lib库所在路径 相当于项目属性配置-链接器-输入-附加依赖项
                ],
            },
        },

    ]
}

  test.cpp

#include <nan.h>
#include <iostream>
#include <fstream>
#include "../Account.pb.h"

using namespace std;

void test()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    IM::Account account1;
    account1.set_id(1);
    account1.set_name("windsun");
    account1.set_password("123456");

    string serializeToStr;
    account1.SerializeToString(&serializeToStr);
    cout << "SerializeToString:" << serializeToStr << endl;

    IM::Account account2;
    if (!account2.ParseFromString(serializeToStr))
    {
        cerr << "failed to parse student." << endl;
    }
    else
    {
        cout << "ParseFromString:" << endl;
        cout << account2.id() << endl;
        cout << account2.name() << endl;
        cout << account2.password() << endl;
        google::protobuf::ShutdownProtobufLibrary();
    }
}

void Method(const Nan::FunctionCallbackInfo<v8::Value> &info)
{
    test();
    info.GetReturnValue().Set(Nan::New("End").ToLocalChecked());
}

void Init(v8::Local<v8::Object> exports)
{
    v8::Local<v8::Context> context = exports->CreationContext();
    exports->Set(context,
                 Nan::New("hello").ToLocalChecked(),
                 Nan::New<v8::FunctionTemplate>(Method)
                     ->GetFunction(context)
                     .ToLocalChecked());
}

NODE_MODULE(hello, Init)

 test.js

var addon = require('bindings')('test');

console.log(addon.hello());

 

重编命令: node-gyp rebuild

 测试: node test/test.js

(以下是【c++】c++项目使用protobuf 运行效果图 )

 

可以看到,无论是C++测试工程使用还是js调用nodejs的c++插件使用,都是一样的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值