﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-下雪天-文章分类-C/C++</title><link>http://www.cppblog.com/longjun-9/category/20575.html</link><description>简单编程</description><language>zh-cn</language><lastBuildDate>Thu, 06 Apr 2017 05:57:24 GMT</lastBuildDate><pubDate>Thu, 06 Apr 2017 05:57:24 GMT</pubDate><ttl>60</ttl><item><title>用C++给nodejs编写模块</title><link>http://www.cppblog.com/longjun-9/articles/214423.html</link><dc:creator>Long</dc:creator><author>Long</author><pubDate>Fri, 18 Nov 2016 12:58:00 GMT</pubDate><guid>http://www.cppblog.com/longjun-9/articles/214423.html</guid><wfw:comment>http://www.cppblog.com/longjun-9/comments/214423.html</wfw:comment><comments>http://www.cppblog.com/longjun-9/articles/214423.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/longjun-9/comments/commentRss/214423.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/longjun-9/services/trackbacks/214423.html</trackback:ping><description><![CDATA[官方教程在此：<a href="https://nodejs.org/api/addons.html">https://nodejs.org/api/addons.html<br /></a><br />因为我想用VS 2015 Community去调试C++模块，所以先安装了VS 2015 Community，详情见<a href="http://www.cppblog.com/longjun-9/articles/214422.html">http://www.cppblog.com/longjun-9/articles/214422.html</a><br /><br />然后安装nodejs，然后安装node-gyp。<br /><br />关于node-gyp的问题比较多，如果遇到问题，最好还是参考github上node-gyp主页的相关说明<a href="https://github.com/nodejs/node-gyp">https://github.com/nodejs/node-gyp</a><br /><br />需要重点注意的部分如下：<br /><br /><h2>Installation</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;">You can install with&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">npm</code>:</p><div highlight-source-shell"="" style="box-sizing: border-box; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;"><pre style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; border-radius: 3px; word-break: normal; background-color: #f7f7f7;">$ npm install -g node-gyp</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;">You will also need to install:</p><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;"><li style="box-sizing: border-box;">On Unix:<ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 0px;"><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">python</code>&nbsp;(<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">v2.7</code>&nbsp;recommended,&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">v3.x.x</code>&nbsp;is&nbsp;<span style="box-sizing: border-box; font-weight: 600;"><em style="box-sizing: border-box;">not</em></span>&nbsp;supported)</li><li style="box-sizing: border-box; margin-top: 0.25em;"><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">make</code></li><li style="box-sizing: border-box; margin-top: 0.25em;">A proper C/C++ compiler toolchain, like&nbsp;<a href="https://gcc.gnu.org/" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">GCC</a></li></ul></li><li style="box-sizing: border-box; margin-top: 0.25em;">On Mac OS X:<ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 0px;"><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">python</code>&nbsp;(<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">v2.7</code>&nbsp;recommended,&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">v3.x.x</code>&nbsp;is&nbsp;<span style="box-sizing: border-box; font-weight: 600;"><em style="box-sizing: border-box;">not</em></span>&nbsp;supported) (already installed on Mac OS X)</li><li style="box-sizing: border-box; margin-top: 0.25em;"><a href="https://developer.apple.com/xcode/download/" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">Xcode</a><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 0px;"><li style="box-sizing: border-box;">You also need to install the&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">Command Line Tools</code>&nbsp;via Xcode. You can find this under the menu&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">Xcode -&gt; Preferences -&gt; Downloads</code></li><li style="box-sizing: border-box; margin-top: 0.25em;">This step will install&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">gcc</code>&nbsp;and the related toolchain containing&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">make</code></li></ul></li></ul></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">On Windows:</p><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 0px;"><li style="box-sizing: border-box;">Option 1: Install all the required tools and configurations using Microsoft's&nbsp;<a href="https://github.com/felixrieseberg/windows-build-tools" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">windows-build-tools</a>&nbsp;using&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">npm install --global --production windows-build-tools</code>&nbsp;from an elevated PowerShell or CMD.exe (run as Administrator).</li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Option 2: Install tools and configuration manually:</p><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 0px;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Visual C++ Build Environment:</p><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 0px;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Option 1: Install&nbsp;<a href="http://landinghub.visualstudio.com/visual-cpp-build-tools" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">Visual C++ Build Tools</a>&nbsp;using the&nbsp;<span style="box-sizing: border-box; font-weight: 600;">Default Install</span>&nbsp;option.</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Option 2: Install&nbsp;<a href="https://www.visualstudio.com/products/visual-studio-community-vs" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">Visual Studio 2015</a>&nbsp;(or modify an existing installation) and select&nbsp;<em style="box-sizing: border-box;">Common Tools for Visual C++</em>&nbsp;during setup. This also works with the free Community and Express for Desktop editions.</p></li></ul><blockquote style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px 1em; color: #777777; border-left-width: 0.25em; border-left-color: #dddddd;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;"><g-emoji alias="bulb" fallback-src="https://assets-cdn.github.com/images/icons/emoji/unicode/1f4a1.png" ios-version="6.0" style="box-sizing: border-box; font-family: &quot;Apple Color Emoji&quot;, &quot;Segoe UI&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 18px; line-height: 20px; vertical-align: middle;"><img alt=":bulb:" height="20" width="20" src="https://assets-cdn.github.com/images/icons/emoji/unicode/1f4a1.png" style="box-sizing: content-box; border-style: none; max-width: none; vertical-align: text-top;" /></g-emoji>&nbsp;[Windows Vista / 7 only] requires&nbsp;<a href="http://www.microsoft.com/en-us/download/details.aspx?id=40773" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">.NET Framework 4.5.1</a></p></blockquote></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Install&nbsp;<a href="https://www.python.org/downloads/" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">Python 2.7</a>&nbsp;(<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">v3.x.x</code>&nbsp;is not supported), and run&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">npm config set python python2.7</code>&nbsp;(or see below for further instructions on specifying the proper Python version and path.)</p></li><li style="box-sizing: border-box; margin-top: 0.25em;">Launch cmd,&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">npm config set msvs_version 2015</code></li></ul></li></ul><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">If the above steps didn't work for you, please visit&nbsp;<a href="https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules" style="box-sizing: border-box; color: #4078c0; text-decoration: none;">Microsoft's Node.js Guidelines for Windows</a>&nbsp;for additional tips.</p></li></ul><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;">If you have multiple Python versions installed, you can identify which Python version&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">node-gyp</code>&nbsp;uses by setting the '--python' variable:</p><div highlight-source-shell"="" style="box-sizing: border-box; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;"><pre style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; border-radius: 3px; word-break: normal; background-color: #f7f7f7;">$ node-gyp --python /path/to/python2.7</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;">If&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">node-gyp</code>&nbsp;is called by way of&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">npm</code>&nbsp;<em style="box-sizing: border-box;">and</em>&nbsp;you have multiple versions of Python installed, then you can set&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">npm</code>'s 'python' config key to the appropriate value:</p><div highlight-source-shell"="" style="box-sizing: border-box; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;"><pre style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; border-radius: 3px; word-break: normal; background-color: #f7f7f7;">$ npm config <span style="box-sizing: border-box; color: #0086b3;">set</span> python /path/to/executable/python2.7</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;">Note that OS X is just a flavour of Unix and so needs&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">python</code>,&nbsp;<code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">make</code>, 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 -&gt; Downloads).<br /><br />网上关于用C++给nodejs写模块的教程很多，如果英文不太好，看不太懂上面官网的内容，可以参考一下中文教程。<br /><br />我自己的文件结构如下：<br /><img src="http://www.cppblog.com/images/cppblog_com/longjun-9/微信截图_20161118204027.png" width="663" height="130" alt="" /><br />执行<span style="font-size: 13px; font-family: verdana, &quot;courier new&quot;; line-height: 21px; background-color: #eeeeee;">node</span><span style="font-size: 13px; font-family: verdana, &quot;courier new&quot;; line-height: 21px; background-color: #eeeeee;">-</span><span style="font-size: 13px; font-family: verdana, &quot;courier new&quot;; line-height: 21px; background-color: #eeeeee;">gyp&nbsp;configure&nbsp;build</span>（两个步骤一起执行），然后就会生成build文件夹，下面的文件结构如下：<br /><img src="http://www.cppblog.com/images/cppblog_com/longjun-9/微信截图_20161118204612.png" width="629" height="234" alt="" /><br />生成的bindings.sln就是在VS 2015下面要打开的工程，接下来就可以用VS来写模块了。<br /><br />需要注意的是，需要安装node-gyp需要安装python2.7，还要添加python的环境变量：<br /><img src="http://www.cppblog.com/images/cppblog_com/longjun-9/微信截图_20161118205153.png" width="669" height="312" alt="" /><br /></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; line-height: 24px;">而且3.X是不行的。最后就是用VS build的时候，VS是开启的debug模式，所以会找不到node-gyp生成的node.lib（因为上面的命令生成的是release库，具体请参考用户目录下的.node-gyp文件夹，里面只有release文件夹而没有debug），所以要想在debug模式下build成功，可以用<span style="font-size: 13px; font-family: verdana, &quot;courier new&quot;; line-height: 21px; background-color: #eeeeee;">node</span><span style="font-size: 13px; font-family: verdana, &quot;courier new&quot;; line-height: 21px; background-color: #eeeeee;">-</span><span style="font-size: 13px; font-family: verdana, &quot;courier new&quot;; line-height: 21px; background-color: #eeeeee;">gyp&nbsp;configure&nbsp;build --debug<br /></span>命令来生成对应的debug库。<br /><br />node-gyp github上的原句：<br /><span style="box-sizing: border-box; font-weight: 600; background-color: #ffffff;">Note:</span><span style="background-color: #ffffff;">&nbsp;To create a&nbsp;</span><em style="box-sizing: border-box; background-color: #ffffff;">Debug</em><span style="background-color: #ffffff;">&nbsp;build of the bindings file, pass the&nbsp;</span><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">--debug</code><span style="background-color: #ffffff;">&nbsp;(or&nbsp;</span><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">-d</code><span style="background-color: #ffffff;">) switch when running either the&nbsp;</span><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">configure</code><span style="background-color: #ffffff;">,</span><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">build</code><span style="background-color: #ffffff;">&nbsp;or&nbsp;</span><code style="box-sizing: border-box; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0px; margin: 0px; border-radius: 3px; background-color: rgba(0, 0, 0, 0.0392157);">rebuild</code><span style="background-color: #ffffff;">&nbsp;command.</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.cppblog.com/longjun-9/aggbug/214423.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/longjun-9/" target="_blank">Long</a> 2016-11-18 20:58 <a href="http://www.cppblog.com/longjun-9/articles/214423.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>通过VS2015 Community 学习《Primer C++》</title><link>http://www.cppblog.com/longjun-9/articles/214422.html</link><dc:creator>Long</dc:creator><author>Long</author><pubDate>Fri, 18 Nov 2016 11:59:00 GMT</pubDate><guid>http://www.cppblog.com/longjun-9/articles/214422.html</guid><wfw:comment>http://www.cppblog.com/longjun-9/comments/214422.html</wfw:comment><comments>http://www.cppblog.com/longjun-9/articles/214422.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/longjun-9/comments/commentRss/214422.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/longjun-9/services/trackbacks/214422.html</trackback:ping><description><![CDATA[想用C++给nodejs写模块，但是C++已经好久不动了，所以装了个VS2015 Community，然后在命令行下面编译运行C++程序。<br /><br />以前都是直接用VS去学习C++，这次是第一次离开IDE环境而改用命令行。<br /><br />首先去官网下载VS2015 Community安装，安装的过程还是挺漫长的，而且默认不会安装Visual C++组件。不安装这个组件会不会导致命令行下编译失败，我没有去测试，但我觉得可能会编译失败。其实一开始安装VS2015 Community的时候，就会有可供选择的组件界面；而我是在安装后新建了一个Visual C++ project，然后按照提示装上了C++组件，但是时间也是很漫长，不知道是自己网速原因还是微软官网本来就不稳定。<br /><br />安装完成后，就可以用VS2015 Community来进行C++编程了。但是想要用命令行编译《Primer C++》上面的例子，还得增加一些环境变量。<br /><br />（顺便提一下，我的操作系统是64位的Win10，下载VS的时候，对应选择的就是X64的版本。）<br /><br />《Primer C++》中编译的命令是&nbsp;<span style="font-size: 13px; background-color: #eeeeee;">cl&nbsp;</span><span style="font-size: 13px; background-color: #eeeeee;">/</span><span style="font-size: 13px; background-color: #eeeeee;">EHsc&nbsp;ex01.cpp</span>，需要添加环境变量到PATH下面。<br />我的cl.exe对应的路径是C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin，添加后如图：<br /><img src="http://www.cppblog.com/images/cppblog_com/longjun-9/微信截图_20161118194542.png" width="632" height="666" alt="" /><br /><br />有了环境变量之后，编写一个测试的小程序ex01.cpp，执行命令<span style="font-size: 13px; background-color: #eeeeee;">cl&nbsp;</span><span style="font-size: 13px; background-color: #eeeeee;">/</span><span style="font-size: 13px; background-color: #eeeeee;">EHsc&nbsp;ex01.cpp</span>，结果还是不行，比如我在文件里包含了iostream，则报错：<br /><div>fatal error C1034: iostream: no include path set<br /><br />经过一番查找，我发现必须运行vcvars32.bat之后，才能通过编译。而这个文件就在C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin下面，和cl.exe同目录。<br /><br />而且这个批处理文件还比较特殊，在该目录下双击运行还不行，得在cmd窗口下执行vcvars32；一旦这个窗口被关闭，下一次开新窗口的时候，又需要再在命令行下面执行一次这个批处理文件，才能正确编译cpp文件。</div><div></div><img src ="http://www.cppblog.com/longjun-9/aggbug/214422.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/longjun-9/" target="_blank">Long</a> 2016-11-18 19:59 <a href="http://www.cppblog.com/longjun-9/articles/214422.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于Swich-Case语句的一个小知识</title><link>http://www.cppblog.com/longjun-9/articles/201311.html</link><dc:creator>Long</dc:creator><author>Long</author><pubDate>Wed, 26 Jun 2013 01:51:00 GMT</pubDate><guid>http://www.cppblog.com/longjun-9/articles/201311.html</guid><wfw:comment>http://www.cppblog.com/longjun-9/comments/201311.html</wfw:comment><comments>http://www.cppblog.com/longjun-9/articles/201311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/longjun-9/comments/commentRss/201311.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/longjun-9/services/trackbacks/201311.html</trackback:ping><description><![CDATA[在如下swicth语句中，<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><span style="color: #0000FF; ">switch</span><span style="color: #000000; ">（value）<br />{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">case</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoSomething();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">case</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">case</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">2</span><span style="color: #000000; ">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoOthers();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;</span><span style="color: #000000; ">&nbsp;&nbsp; <br /></span><span style="color: #0000FF; ">&nbsp; default</span><span style="color: #000000; ">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;<br />}</span><span style="color: #000000; "></span></div>如果<span style="color: #0000FF; ">case</span><span style="color: #000000; "></span> 1语句后面没有跟break关键字，程序会一直往下匹配到case 2 的break，而把<span style="color: #000000; ">&nbsp; </span><span style="color: #0000FF; ">case</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">2</span><span style="color: #000000; ">: DoOthers(); 作为case 1语句中的代码块执行，其结果是：当value=1时，</span><span style="color: #000000;">DoOthers()也被执行了。<br />我想要强调的是，除非是你刻意要达到这种效果，否则不要掉了case 后面的break关键字，不然会产生一些意想不到的执行结果<br /></span><img src ="http://www.cppblog.com/longjun-9/aggbug/201311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/longjun-9/" target="_blank">Long</a> 2013-06-26 09:51 <a href="http://www.cppblog.com/longjun-9/articles/201311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>