c++compilationc++17c++20node-gyp

I can’t seem to use std::optional with node-gyp


Every time I try to compile my C++ code that uses std::optional with node-gyp, I get:

error: no template named 'optional' in namespace 'std'

I assumed the problem was with the C++ standard (since I am obviously including the <optional> header in my code), and I’ve tried to set it to C++20 (or 17) with cflags_cc, xcode_settings.CLANG_CXX_LANGUAGE_STANDARD, xcode_settings.OTHER_CFLAGS, etc.… Everything I could find, basically, yet I can’t get rid of that error.

Here’s my binding.gyp for reference:

{
    "targets": [
        {
            "target_name": "clip_frame_cropper",
            "includes": ["auto.gypi"],
            "sources": ["./src/C++/clip_frame_cropper.cpp"],
            "include_dirs": ["/usr/local/include/opencv4"],
            "libraries": ["-lopencv_core", "-lopencv_imgproc", "-lopencv_videoio", "-lopencv_imgcodecs"],
            "cflags_cc": ["-std=c++20", "-Ofast"],
            "xcode_settings": {
                "CLANG_CXX_LANGUAGE_STANDARD": "c++20",
                "OTHER_CFLAGS": ["-std=c++20"],
                "OTHER_CPLUSPLUSFLAGS": ["-std=c++20"]
            }
        }
    ],
    "includes": ["auto-top.gypi"]
}

I am on the latest version of macOS… Also, the code compiles just fine outside of node-gyp, for what it’s worth.

By the way, literally any code that uses std::optional causes this issue, so it’s not my code that I am asking about, but rather node-gyp. Yes, even something like this:

#include <optional>

std::optional<int> test;

Solution

  • Okay, I was able to fix it thanks to the user who suggested passing the -v flag to get more info.

    I realized the compiler was being executed with -std=c++11 regardless of what flags I used, so I fixed it by adding the following field:

    "OTHER_CPLUSPLUSFLAGS!": ["-std=c++11"]
    

    The “!” is supposed to tell node-gyp to not use that flag. No idea why that’s necessary and I haven’t seen it anywhere else, but maybe it’ll help someone in the future.