Node-gyp: Is there any way to use C++17 on MSBuild?

Created on 7 Feb 2019  ·  13Comments  ·  Source: nodejs/node-gyp

  • Node Version:10.15.0
  • Platform:Windows 10 x64
  • Compiler:MSBuild ,toolset v150
  • Module:my SVLauncherDLL

Verbose output (from npm or node-gyp):

e:\visual studio项目\dsted\sourcecpp-addon\svlaunchdll_jswrap.cc(135): error C7525: 内联变量至少需要 "/std:c++17" (编译源文件 ..cpp-
addon\svlaunchdll_jswrap.cc) [E:\Visual Studio项目\DSTed\Source\build\SVLaunchDLL.vcxproj]


I need C++17, seriously.

cpp-addon.zip

Windows question

Most helpful comment

Add the following to your binding.gyp:

  'msvs_settings': {
    'VCCLCompilerTool': {
      'AdditionalOptions': [ '-std:c++17', ],
    },
  },

All 13 comments

Add the following to your binding.gyp:

  'msvs_settings': {
    'VCCLCompilerTool': {
      'AdditionalOptions': [ '-std:c++17', ],
    },
  },

Failed

Traceback (most recent call last):
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\gyp_main.py", line 16, in <module>
    sys.exit(gyp.script_main())
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 545, in script_main
    return main(sys.argv[1:])
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 538, in main
    return gyp_main(args)
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 514, in gyp_main
    options.duplicate_basename_check)
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 130, in Load
    params['parallel'], params['root_targets'])
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 2783, in Load
    variables, includes, depth, check, True)
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 399, in LoadTargetBuildFile
    includes, True, check)
  File "E:\NPM_PREFIX\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 251, in LoadOneBuildFile
    None)
  File "binding.gyp", line 23
    'msvs-settings':
                  ^
SyntaxError: invalid syntax

binding.gyp.txt

replace [ with { for 'msvs_settings' and 'VCCLCompilerTool'
binding.gyp.txt

Verbose output (from npm or node-gyp):
Traceback (most recent call last):
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\gyp_main.py", line 16, in
sys.exit(gyp.script_main())
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gyp__init__.py", line 545, in script_main
return main(sys.argv[1:])
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gyp__init__.py", line 538, in main
return gyp_main(args)
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gyp__init__.py", line 514, in gyp_main
options.duplicate_basename_check)
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gyp__init__.py", line 130, in Load
params['parallel'], params['root_targets'])
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gypinput.py", line 2783, in Load
variables, includes, depth, check, True)
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gypinput.py", line 425, in LoadTargetBuildFile
build_file_data, PHASE_EARLY, variables, build_file_path)
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gypinput.py", line 1293, in ProcessVariablesAndConditionsInDict
build_file, key)
File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gypinput.py", line 1303, in ProcessVariablesAndConditionsInDict
' for ' + key)
TypeError: Unknown type set for AdditionalOptions while trying to load binding.gyp

binding.gyp.txt

i had changed binding.gyp as you said, but it does not work

please try and use Refael's exact syntax:

            'msvs-settings':
            {
                'VCCLCompilerTool':
                {
                    'AdditionalOptions':
                        [
                        '-std:c++17',
                        ]
                }
            }

note the placement of {' and ['s

@rvagg still not working

This is a full working example for anyone who also has this problem:

{
  "targets": [
    {
      "target_name": "proj",
      "sources": [ "src/proj.cc" ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
      "cflags": [ "-fno-exceptions" ],
      "cflags_cc": [ "-fno-exceptions", "-std=c++17" ],
      "conditions": [
        ['OS=="mac"', {
            "xcode_settings": {
              'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
              "CLANG_CXX_LIBRARY": "libc++",
              "CLANG_CXX_LANGUAGE_STANDARD":"c++17",
              'MACOSX_DEPLOYMENT_TARGET': '10.14'
            }
        }],
        ['OS=="win"', {
          "msvs_settings": {
            "VCCLCompilerTool": {
              "AdditionalOptions": [ "-std:c++17", ],
            },
          },
        }]
      ]
    }
  ]
}


The solutions above didn't work for me - the only way I could get it to work was using

"msbuild_settings": {
    "ClCompile": {
        "LanguageStandard": "stdcpp17"
    }
}

It thows off some warnings, but it configures the project as it should and compiles

The solutions above didn't work for me - the only way I could get it to work was using

"msbuild_settings": {
    "ClCompile": {
        "LanguageStandard": "stdcpp17"
    }
}

It thows off some warnings, but it configures the project as it should and compiles

Yes. ClCompile MSBuild item controls how compiler treats source file

The solutions above didn't work for me

That's strange. None of the given solutions sets the standard in the sln file for the analysis. But it works for the build.

The solutions above didn't work for me

That's strange. None of the given solutions sets the standard in the sln file for the analysis. But it works for the build.

see .vcxproj file?

If anyone need solution irrespective of node-gyp use can use cmake... You can do this using cmake-js node package.
It easily handle these kind of things. Here is a example for you: https://www.github.com/AtiqGauri/Patternscape/tree/master/Patternscape_Native_Addon%2FCMakeLists.txt

The solutions above didn't work for me

That's strange. None of the given solutions sets the standard in the sln file for the analysis. But it works for the build.

see .vcxproj file?

Node-gyp should be able to do this automatically, but it does not. CMake easily does it by using.

set(CMAKE_CXX_STANDARD 17)

If anyone need solution irrespective of node-gyp use can use cmake... You can do this using cmake-js node package.
It easily handle these kind of things. Here is a example for you:

Thanks! I need to transfer to cmake-js. Too many problems with node-gyp. It does not solve anything for me as I have to manually maintain compiler flags for all the platforms.

Was this page helpful?
0 / 5 - 0 ratings