Skip to content

Is there any way to use C++17 on MSBuild? #1662

Closed

Description

@Akarinnnnn
  • 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\source\cpp-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

Activity

  1. refack commented on Feb 7, 2019

    @refack
    Contributor

    Add the following to your binding.gyp:

      'msvs_settings': {
        'VCCLCompilerTool': {
          'AdditionalOptions': [ '-std:c++17', ],
        },
      },
  2. Akarinnnnn commented on Feb 7, 2019

    @Akarinnnnn
    Author

    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

  3. refack commented on Feb 7, 2019

    @refack
    Contributor

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

  4. Akarinnnnn commented on Feb 8, 2019

    @Akarinnnnn
    Author
    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\gyp\input.py", line 2783, in Load variables, includes, depth, check, True) File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gyp\input.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\gyp\input.py", line 1293, in ProcessVariablesAndConditionsInDict build_file, key) File "E:\NPM_PREFIX\node_modules\prebuild\node_modules\node-gyp\gyp\pylib\gyp\input.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

  5. rvagg commented on Jun 20, 2019

    @rvagg
    Member

    please try and use Refael's exact syntax:

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

    note the placement of {' and ['s

  6. AtiqGauri commented on Jul 6, 2019

    @AtiqGauri

    @rvagg still not working

  7. aminya commented on Nov 1, 2020

    @aminya

    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", ],
                },
              },
            }]
          ]
        }
      ]
    }
    
  8. mceIdo commented on Jan 4, 2021

    @mceIdo

    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

  9. Akarinnnnn commented on Jan 5, 2021

    @Akarinnnnn
    Author

    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

  10. aminya commented on Jan 6, 2021

    @aminya

    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.

  11. Akarinnnnn commented on Jan 6, 2021

    @Akarinnnnn
    Author

    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?

  12. AtiqGauri commented on Jan 6, 2021

    @AtiqGauri

    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

  13. aminya commented on Jan 6, 2021

    @aminya

    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.

  14. ToadKing commented on Jan 12, 2021

    @ToadKing

    Ran into this issue myself. Setting it through "msbuild_settings" worked but I think doing it that way messes up the node.exe hooking stuff since the compiled module would not load in electron. It also changes some other flags as well since the module no longer built as a static binary.

    @refack's original suggestion worked for me. That said, the "VCCLCompilerTool" option should probably be updated to allow setting "LanguageStandard" directly instead of adding it through an additional command line option.

  15. julianmesa-gitkraken commented on Oct 6, 2022

    @julianmesa-gitkraken
             "msvs_settings": {
                "VCCLCompilerTool": {
                  "AdditionalOptions": [
                    "/std:c++17"
                  ]
                }
              },
    
  16. lirf233 commented on Nov 9, 2023

    @lirf233

    Add the following to your binding.gyp:

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

    nice, it works!!

  17. DanielCaspers commented on Sep 3, 2024

    @DanielCaspers

    For those coming here trying to get C++ 20 working with node addons relying on either Node directly or a derivative (e.g. Electron), don't forget that on Windows you will need to specify /Zc:__cplusplus. It took a while for me to discover this sharp windows specific edge...
    https://stackoverflow.com/questions/63057156/language-supported-in-visual-studio-for-c-cplusplus-is-showing-as-c98/63057345#63057345

  18. Psychlist1972 commented on Sep 6, 2024

    @Psychlist1972

    In my case, the -std:c++17 didn't work with VS 2022 and MS Build. I had to use the CLCompile version mentioned above.

    I was banging my head up against the wall trying to find out how to override the C++20 that node-gyp was putting into the vcxprj but never could locate the source.

    This is all because NodeRT uses C++ CX to create the projections, and that is not compatible with C++ 20.

        "msbuild_settings":{
            "ClCompile":{
                "LanguageStandard": "stdcpp17",
                "AdditionalOptions": "/ZW",
                "AdditionalUsingDirectories": [
                  "%ProgramFiles(x86)%/Microsoft Visual Studio 14.0/VC/lib/store/references",
                  "%ProgramFiles%/Microsoft Visual Studio 14.0/VC/lib/store/references",
                  "%ProgramFiles(x86)%/Windows Kits/10/UnionMetadata/10.0.20348.0",
                  "$(VCToolsInstallDir)/lib/x86/store/references",
                  "../../",
                ]
    
            }
        }

    I still have this in the binding.gyp, but it is overridden by the msbuild settings above

        "msvs_settings": {
          "VCCLCompilerTool": {
            "AdditionalOptions": ["/ZW", "-std:c++17"],
            "DisableSpecificWarnings": [4609],
          }
        },
  19. Psychlist1972 commented on Sep 6, 2024

    @Psychlist1972

    For those coming here trying to get C++ 20 working with node addons relying on either Node directly or a derivative (e.g. Electron), don't forget that on Windows you will need to specify /Zc:__cplusplus. It took a while for me to discover this sharp windows specific edge... https://stackoverflow.com/questions/63057156/language-supported-in-visual-studio-for-c-cplusplus-is-showing-as-c98/63057345#63057345

    In my case, node-gyp was adding both that and the /std:c++20 to the project file by default, with no obvious way to override.

  20. BFjacky commented on Dec 6, 2024

    @BFjacky

    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", ],
                },
              },
            }]
          ]
        }
      ]
    }

    Worked with this config !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions