vscode 编写node的c++ 扩展

前言

在此介绍一下node的c++扩展在vscode 上的编译环境,在此不多说,比较完善,看了肯定明白。

正文

c++ 环境搭建

下载mingw,然后配置好环境。下载地址为,官网,可以自己百度一下。

配置环境很简单,就是把/bin 放在环境变量中。

那么就看vscode 配置c++ 环境呢?

装好c++插件:

vscode 编写node的c++ 扩展

在.vscode 文件下,创建c_cpp_properties.json

如下:

{
    "configurations": [
        {
            "name": "windows",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "D:\mingw64\bin\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

compilerPath 就是你的mingw64配置位置;

接下来你就可以去编写你的c++代码了。写上,#incluent ,如果没有错误表示成功。

那么接下来就是配置node 扩展环境,好吧,那就开始吧。

node 环境

https://github.com/nodejs/node/tags 下载你的node版本的源码包,我的是14.15.5。

接下来就是解压,这个不用多说。

解压后内部就是这样子了。

那么我们接下来就是配置环境。

首先我们要知道node 是基于v8的,而我们的扩展其实也是基于v8的,所以要配置一下v8。

那么v8在哪里呢?看上面目录中有一个deps,这个是node 的依赖,里面就有v8了。

接来下配置就是这样子的了:

{
    "configurations": [
        {
            "name": "windows",
            "includePath": [
                "${workspaceFolder}/**",
                "D:\node-14.15.5\deps\v8\include\",
                "D:\node-14.15.5\src\"
            ],
            "defines": [],
            "compilerPath": "D:\mingw64\bin\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

includePath 里面增加了v8和node 配置哈,本来node 里面include就可以,但是我们可以看到源码还是很开心的,方便后面调试。

然后你在文件中写入:#include <node.h>,发现没有错误,那么恭喜你成功。

写代码是没得问题了,但是我们还得安装一下编译环境吧,对吧。

npm install -g node-gyp

接下来在hello.cc 中,复制一下官网给的例子:

// 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").ToLocalChecked());
}

void Initialize(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

}  // 命名空间示例

现在代码和环境好了,那么还得有gyp环境的配置,我们比如告诉它如何编译,从哪个开始。

在根目录下创建,binding.gyp,写入下面的:

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [ "hello.cc" ]
    }
  ]
}

然后我们写入npm init,进行初始化,初始化如下:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "install": "node-gyp rebuild"
  },
  "author": "",
  "license": "ISC",
  "gypfile": true
}

发现script下有一个install,那么来编译一下,node-gyp rebuild。

然后我们就发现构建完了。

那么如何调用呢?官网给出例子:

// hello.js
const addon = require('./build/Release/addon');

console.log(addon.hello());

这个可以自己试一下。

vscode 编写node的c++ 扩展

我的实验如上,返回word。好的,那么我们的code是成功的,但是我希望的是能够调试。

node c++ 插件调试

这个还是比较简单的,下载插件:lldb。
package.json 中script 加入:

"build:dev": "node-gyp build --debug"

在task.json中加入:

 {
            "type": "npm",
            "script": "build:dev",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [],
            "label": "npm: build:dev",
            "detail": "node-gyp build --debug"
}

然后配置文件中调试配置:

{
	"name": "Launch Program",
	"request": "launch",
	"type": "lldb",
	"preLaunchTask": "npm: build:dev",
	"program": "node",
	"args": [
	   "${workspaceFolder}/test.js"
	]
 }

解释一下,其实就是preLaunchTask 出发前面那个任务,那个任务就是去编译一下,然后debug类型换成lldb,即可。

效果如下:

vscode 编写node的c++ 扩展

后续更新c++基础,写一些c++ node插件c++基础就好,因为一般都是调用库,而不是去从头写,那是只有职业c++干的事,当然如果我们热爱c++,那么可能会变成职业的。