node-gyp ignores (c++17) cflag

Viewed 1406

I try to configure & build a node.js C++ addon with this binding.gyp file:

{ 
  "targets": [
    {
      "target_name": "addon",
      "sources": [ "addon.cpp" ],
      "cflags": [
        "-std=c++17"
      ]          
    }
  ]
}

But when I run node-gyp configure and node-gype rebuild I always get messages like

warning: ‘if constexpr’ only available with -std=c++17 or -std=gnu++17

The build also fails, because I really depend on these c++17 features. What am I doing wrong?

2 Answers

cflags cflags_cc was not working for me, but with the setting in VCCLCompilerTool it works (on Windows):

{
  'targets': [
    {
      'target_name': 'test-napi-native',
      'sources': [ 'src/test_napi.cc' ],
      'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
      'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
      'cflags': [ '-fno-exceptions' ],
      'cflags_cc': [ '-fno-exceptions' ],
      'xcode_settings': {
        'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
        'CLANG_CXX_LIBRARY': 'libc++',
        'MACOSX_DEPLOYMENT_TARGET': '10.7'
      },
      'msvs_settings': {
        'VCCLCompilerTool': { "ExceptionHandling": 1, 'AdditionalOptions': [ '-std:c++17' ] }
      }
    }
  ]
}

Using "cflags_cc" (instead of "cflags") works.

This solved the problem.

Related