4

I want to compile a node.js module with the /MD flag (multi-threaded DLL). Having '/MD' in the cflags options in binding.gyp does not work.

Remi Arnaud
  • 465
  • 1
  • 4
  • 11

3 Answers3

4

After playing around with binding.gyp - a lot - it seems as though the problem is not due node-gyp but to do with gyp itself and the very specific nesting order which it requires for particular settings. That is, for setting the runtime library (in release), the runtime library option must be nested within the gyp file as:

configurations
  - Release
    - msvs_settings
      - VCCLCompilerTool
        - RuntimeLibrary

Trying to set the runtime library without any one of these nesting elements stops the runtime library from being set. (Annoyingly without any sort of warning that this option is ignored.)

Therefore to set the debug and release builds of a module to use the debug runtime DLLs (compiler option /MDd) and release runtime DLLs (compiler option /MD), the binding.gyp will be as follows:

{
    'targets': [
    {
        # Usual target name/sources, etc.

        'configurations': {
            'Debug': {
                'msvs_settings': {
                            'VCCLCompilerTool': {
                                'RuntimeLibrary': '3' # /MDd
                    },
                },
            },
            'Release': {
                'msvs_settings': {
                            'VCCLCompilerTool': {
                                'RuntimeLibrary': '2' # /MD
                    },
                },
            },
        },
    },],
}
neurotempest
  • 147
  • 5
3

You're gonna need to set RuntimeLibrary to 2. Something like this:

'msvs_settings': {
  'VCCLCompilerTool': {
    'RuntimeLibrary': 2, # multi threaded DLL
  },
},
TooTallNate
  • 1,477
  • 3
  • 20
  • 41
  • Thanks, this is the right option. But I had to modify the file ~/.node-gyp/0.8.9/common.gypi, which resets whatever settings I have in the binding.gyp file. Do I really have to have my own version of node-gyp to make this work, or am I missing something? – Remi Arnaud Nov 30 '12 at 01:07
  • too late to edit comment... note that the correct setting is 2 for /MD. 3 is /MDd – Remi Arnaud Nov 30 '12 at 01:25
  • Thanks, updated. As of node-gyp v0.8.0, you can create your _own_ common.gypi alongside your binding.gyp file and define the setting there, and it should take precedence. – TooTallNate Dec 01 '12 at 06:03
  • 1
    I really wonder how to get this working...trying with v0.10.10 and nothing I set in a common.gypi seems to make an impact... Anyone solved this? – Irwin Coleman Sep 20 '13 at 17:11
  • I spent a bit of time searching and the values of RuntimeLibrary look like they are documented here: https://msdn.microsoft.com/en-us/library/aa652367%28v=vs.71%29.aspx – Mike Tunnicliffe Aug 11 '15 at 09:35
0

For my project the only solution was to create a new configuration and inherit from the original config:

  'target_defaults': {
    'configurations': {
      'ChirpDebug' : {
        'inherit_from': ['Debug'],
        'msvs_settings': {
          'VCCLCompilerTool': {
            'RuntimeLibrary': '3'
          },
        },
      },
      'ChirpRelease' : {
        'inherit_from': ['Release'],
        'msvs_settings': {
          'VCCLCompilerTool': {
            'RuntimeLibrary': '2'
          },
        },
      },
    },

and then use

msbuild /p:Configuration=ChirpDebug ....

I tried this solution with libuv and it works well. I don't know about node-gyp, but a similar approach should work.