随笔 - 0  文章 - 1  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿

文章分类

文章档案

搜索

  •  

最新评论

官方教程在此:https://nodejs.org/api/addons.html

因为我想用VS 2015 Community去调试C++模块,所以先安装了VS 2015 Community,详情见http://www.cppblog.com/longjun-9/articles/214422.html

然后安装nodejs,然后安装node-gyp。

关于node-gyp的问题比较多,如果遇到问题,最好还是参考github上node-gyp主页的相关说明https://github.com/nodejs/node-gyp

需要重点注意的部分如下:

Installation

You can install with npm:

$ npm install -g node-gyp

You will also need to install:

  • On Unix:
    • python (v2.7 recommended, v3.x.x is not supported)
    • make
    • A proper C/C++ compiler toolchain, like GCC
  • On Mac OS X:
    • python (v2.7 recommended, v3.x.x is not supported) (already installed on Mac OS X)
    • Xcode
      • You also need to install the Command Line Tools via Xcode. You can find this under the menu Xcode -> Preferences -> Downloads
      • This step will install gcc and the related toolchain containing make
  • On Windows:

    • Option 1: Install all the required tools and configurations using Microsoft's windows-build-tools using npm install --global --production windows-build-tools from an elevated PowerShell or CMD.exe (run as Administrator).
    • Option 2: Install tools and configuration manually:

      • Visual C++ Build Environment:

        • Option 1: Install Visual C++ Build Tools using the Default Install option.

        • Option 2: Install Visual Studio 2015 (or modify an existing installation) and select Common Tools for Visual C++ during setup. This also works with the free Community and Express for Desktop editions.

        :bulb: [Windows Vista / 7 only] requires .NET Framework 4.5.1

      • Install Python 2.7 (v3.x.x is not supported), and run npm config set python python2.7 (or see below for further instructions on specifying the proper Python version and path.)

      • Launch cmd, npm config set msvs_version 2015

    If the above steps didn't work for you, please visit Microsoft's Node.js Guidelines for Windows for additional tips.

If you have multiple Python versions installed, you can identify which Python version node-gyp uses by setting the '--python' variable:

$ node-gyp --python /path/to/python2.7

If node-gyp is called by way of npm and you have multiple versions of Python installed, then you can set npm's 'python' config key to the appropriate value:

$ npm config set python /path/to/executable/python2.7

Note that OS X is just a flavour of Unix and so needs pythonmake, and C/C++. An easy way to obtain these is to install XCode from Apple, and then use it to install the command line tools (under Preferences -> Downloads).

网上关于用C++给nodejs写模块的教程很多,如果英文不太好,看不太懂上面官网的内容,可以参考一下中文教程。

我自己的文件结构如下:

执行node-gyp configure build(两个步骤一起执行),然后就会生成build文件夹,下面的文件结构如下:

生成的bindings.sln就是在VS 2015下面要打开的工程,接下来就可以用VS来写模块了。

需要注意的是,需要安装node-gyp需要安装python2.7,还要添加python的环境变量:

而且3.X是不行的。最后就是用VS build的时候,VS是开启的debug模式,所以会找不到node-gyp生成的node.lib(因为上面的命令生成的是release库,具体请参考用户目录下的.node-gyp文件夹,里面只有release文件夹而没有debug),所以要想在debug模式下build成功,可以用node-gyp configure build --debug
命令来生成对应的debug库。

node-gyp github上的原句:
Note: To create a Debug build of the bindings file, pass the --debug (or -d) switch when running either the configure,build or rebuild command.

posted on 2016-11-18 20:58 Long 阅读(428) 评论(0)  编辑 收藏 引用 所属分类: C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理