Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Akarinnnnn opened this issue Feb 7, 2019 · 20 comments
Closed

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

Akarinnnnn opened this issue Feb 7, 2019 · 20 comments

Comments

@Akarinnnnn
Copy link

  • 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

@refack
Copy link
Contributor

refack commented Feb 7, 2019

Add the following to your binding.gyp:

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

@Akarinnnnn
Copy link
Author

Akarinnnnn commented Feb 7, 2019

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

@refack
Copy link
Contributor

refack commented Feb 7, 2019

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

@Akarinnnnn
Copy link
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

@rvagg
Copy link
Member

rvagg commented Jun 20, 2019

please try and use Refael's exact syntax:

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

note the placement of {' and ['s

@rvagg rvagg closed this as completed Jun 20, 2019
@AtiqGauri
Copy link

@rvagg still not working

@aminya
Copy link

aminya commented Nov 1, 2020

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

@mceIdo
Copy link

mceIdo commented Jan 4, 2021

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

@Akarinnnnn
Copy link
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

@aminya
Copy link

aminya commented Jan 6, 2021

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.

@Akarinnnnn
Copy link
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?

@AtiqGauri
Copy link

AtiqGauri commented Jan 6, 2021

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

@aminya
Copy link

aminya commented Jan 6, 2021

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.

@ToadKing
Copy link

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.

@julianmesa-gitkraken
Copy link

         "msvs_settings": {
            "VCCLCompilerTool": {
              "AdditionalOptions": [
                "/std:c++17"
              ]
            }
          },

@lirf233
Copy link

lirf233 commented Nov 9, 2023

Add the following to your binding.gyp:

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

nice, it works!!

@DanielCaspers
Copy link

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

@Psychlist1972
Copy link

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

@Psychlist1972
Copy link

Psychlist1972 commented Sep 6, 2024

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.

@BFjacky
Copy link

BFjacky commented Dec 6, 2024

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
Projects
None yet
Development

No branches or pull requests