前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NodeJs —— 在Visual Studio中开发C++插件之环境配置

NodeJs —— 在Visual Studio中开发C++插件之环境配置

原创
作者头像
ranky
修改2019-04-29 14:37:37
2.6K0
修改2019-04-29 14:37:37
举报

1,安装必要的库

    a)NodeJs      

  1. https://github.com/nodejs 拷贝代码就可以了
  2. 命令行为  git clone https://github.com/nodejs  本地目录

    b)python 2.x  

  1. 因为Nodejs 只支持python2.x
  2. 添加python环境变量 

   c)安装NASM

     最新版地址:  https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/

     并如b) 添加环境变量

2,编译Nodejs

  1.  如果是只需要release模式,进入node目录,执行vcbuild.bat 即可
  2. 如果是需要debug,进入node目录,执行命令./vcbuild.bat nosign Debug 

3,配置nodejs环境变量 

 这里最好是通过我的电脑->属性->环境变量的方法设置环境变量,在cmd中配置环境变量只是暂时有效。

 4,创建VS工程,并设置项目配置(主要是引用node库)

  1. 创建一个c++空工程
  2. 配置属性->常规:
  3. 配置属性->常规->目标文件扩展名: .node
  4. 配置属性->常规->配置类型: dll
  5. 配置属性->调试: 
  6. 配置属性->调试->命令:$(NodeRoot)\$(Configuration)\node.exe
  7. 配置属性->调试->命令参数:run.js (通过run.js 来加载node插件)
  8. 配置属性->C/C++->常规 :
  9. 附加包含目录:$(NodeRoot)\deps\v8\include;$(NodeRoot)\deps\uv\include;$(NodeRoot)\src
  10. 配置属性->链接器->常规:
  11. $(NodeRoot)\$(Configuration)
  12. 配置属性->链接器->输入: 添加node.lib

5,添加工程代码(以github helloworld为例):

 a)binding.gyp

 这里主要是用于GYP编译使用(例如xcode编译),如果是VS编译可以不用管这个文件。 声明targetname(插件名) 和source( 源cpp文件)

    {

 "targets": [

  {

   "target\_name": "helloworld",

   "sources": [ "hello.cpp" ]

  } ]

  }

 b)hello.cpp

   源文件

   // hello.cc

#include <node.h>

namespace demo {

 using v8::FunctionCallbackInfo;

 using v8::Isolate;

 using v8::Local;

 using v8::Object;

 using v8::String;

 using v8::Value;

 void Method(const FunctionCallbackInfo<Value>& args) {

  Isolate\* isolate = args.GetIsolate();

  args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));

 }

 void init(Local<Object> exports) {

  NODE\_SET\_METHOD(exports, "hello", Method);

 }

 NODE\_MODULE(addon, init)

} // namespace demo

c)package.json(包含模块的一些信息)

main很重要,标识了模块的路径,路径错了,就加载不了

{

 "name": "hellpworld",

 "version": "1.0.0",

 "main": "../x64/Debug/helloworld"

}

d)run.js

js 代码,作为一个参数传给node.exe 的,helloworld 为模块名

var helloworld = require('../x64/Debug/helloworld');

console.warn(helloworld.hello());

setTimeout(function () {alert(2)}, 100000);

相关资料可查看:

nodejs addon 中文网:  http://nodejs.cn/api/addons.html

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档