配置 Windows 下的 Nodejs C++ 模块编译环境

配置 Windows 下的 Nodejs C++ 模块编译环境

根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 “Python + VC++ 编译环境”.

所有需要的安装文件, 我都下载好放到百度云盘了: nodejs-gyp-windows

Python

  • 安装 python-2.7.7.msi

iso 虚拟磁盘

  • 安装 DTLite4491-0356.exe

Windows XP

文件在: node-gyp-windows/windowsXP

  • 用 DTLite 打开 VS2010Express1.iso , 然后安装 Visual Studio C++ 2010 就OK了, 其他不需要安装

Windows 7

文件在: node-gyp-windows/windows7

  • 用 DTLite 打开 VS2012_WDX_ENU.iso , 然后点击 “Install” 就OK了, 会安装整个 Visual Studio Express 2012

编译你的第一个 nodejs c++ 模块

假设你在使用 node v0.10.29

这里举例安装 microtime 模块:

$ npm install microtime --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/dist

测试安装编译结果:

$ node -e 'console.log("now is %d ms", require("microtime").now())'

原文: http://fengmk2.cnpmjs.org/blog/2014/07/node-gyp-cpp-build-env.html


8 回复

配置 Windows 下的 Nodejs C++ 模块编译环境

在 Windows 上配置 Node.js 的 C++ 模块编译环境主要涉及到两个关键组件:Python 和 Visual C++ 编译工具。以下是详细的步骤和示例代码。

安装 Python

首先,确保你已经安装了 Python。你可以从官方网站下载 Python 2.7.7 版本:

# 下载地址: https://www.python.org/downloads/release/python-277/

安装完成后,将 Python 添加到系统路径中:

# 在命令提示符中输入以下命令:
setx PATH "%PATH%;C:\Python27"

安装 Visual C++ 编译工具

你需要安装 Visual Studio 的 C++ 编译工具。以下是具体步骤:

  1. 下载并安装 Visual Studio:
    • 对于 Windows XP 用户,可以下载并安装 Visual Studio 2010 Express。
    • 对于 Windows 7 用户,可以下载并安装 Visual Studio 2012 Express。
# 下载地址: 
# Visual Studio 2010 Express: http://pan.baidu.com/s/1iwrT0
# Visual Studio 2012 Express: http://pan.baidu.com/s/1iwrT0
  1. 安装过程:
    • 使用 DTLite 打开下载的 ISO 文件。
    • 安装过程中选择只安装 C++ 编译工具。

配置 Node-GYP

安装完上述依赖后,需要全局安装 node-gyp 工具,以便编译 Node.js C++ 模块。

# 全局安装 node-gyp
npm install -g node-gyp

编译你的第一个 Node.js C++ 模块

假设你正在使用 Node.js 0.10.29 版本。这里以安装 microtime 模块为例:

# 安装 microtime 模块
npm install microtime --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/dist

安装完成后,可以通过以下命令测试模块是否正确安装和编译:

# 测试安装
node -e 'console.log("now is %d ms", require("microtime").now())'

如果一切正常,你应该能够看到当前时间的毫秒值输出。

原文链接

更多详细信息可以参考原文: http://fengmk2.cnpmjs.org/blog/2014/07/node-gyp-cpp-build-env.html

通过以上步骤,你就可以在 Windows 系统上成功配置 Node.js 的 C++ 模块编译环境,并开始开发和编译你的 Node.js C++ 模块了。


苏老师神文顶起

苏神科普文,学习了~

好文章, 不过我装了 c++ vs express 之后还行不行, 难道是版本不对么, 反正是费老大劲了

我一般都是建议别人安装 vs2010 profession 版本,最靠谱

装个VS,妥妥滴……

作为一个newbie,在node-gyp build时 报错: error C2039: ‘Arguments’: is not a member of ‘v8’;这个什么问题,跟什么有关???

要在 Windows 下配置 Node.js C++ 模块编译环境,你需要安装 Python 和 Visual C++ 编译工具。以下是具体步骤:

  1. 安装 Python:

    • 下载并安装 Python 2.7.7(推荐版本),可以从官网下载安装包。
  2. 安装 Visual C++ 编译环境:

    • 对于 Windows XP 用户,建议安装 Visual Studio C++ 2010。
    • 对于 Windows 7 用户,可以安装 Visual Studio Express 2012。

你可以通过以下步骤来安装 Visual Studio C++ 编译工具:

  • 使用 DT Lite 等虚拟磁盘软件打开相应的 ISO 文件(如 VS2010Express1.isoVS2012_WDX_ENU.iso)。
  • 运行安装程序,选择安装 Visual C++ 编译工具即可。

完成上述安装后,还需要确保全局安装 node-gyp 和配置环境变量。使用管理员权限打开命令提示符,并执行以下命令:

npm install -g node-gyp
npm config set msvs_version 2015 --global  # 如果使用的是较新版本的 Visual Studio

最后,验证安装是否成功:

node-gyp -v

如果显示 node-gyp 版本号,则说明配置成功。

编译第一个 Node.js C++ 模块

假设你想编译一个简单的 C++ 模块 hello.node,可以通过 node-gyp 工具生成 Makefile 并进行编译。

  1. 创建一个名为 binding.gyp 的文件,包含模块定义:
{
  "targets": [
    {
      "target_name": "hello",
      "sources": [ "hello.cc" ]
    }
  ]
}
  1. 创建 hello.cc 文件,包含模块逻辑:
#include <node.h>

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
}

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

NODE_MODULE(hello, Initialize)
  1. 在项目根目录下执行以下命令来编译模块:
node-gyp configure
node-gyp build

完成后,在 build/Release 目录下将生成 hello.node 文件。你可以在 Node.js 中加载和测试这个模块:

const hello = require('./build/Release/hello');
console.log(hello.hello());  // 输出 "world"

这样你就完成了在 Windows 下配置 Node.js C++ 模块编译环境的基本步骤。

回到顶部