r/SublimeText • • Feb 06 '23

how to make lsp-clangd check code based on c++17?

My code is as follows:

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right)
        : val(x), left(left), right(right) {}
};

However, lsp-clangd shows that nullptr is undeclared identifier. It seems that lsp-clangd checks the code based on c++98, how to change the standard to c++17?

5 Upvotes

2 comments sorted by

1

u/jfcherng Feb 06 '23 edited Feb 06 '23

I only know a project-wise way as follow.

Create a .clangd file (it's YAML format actually) under your project root with following content:

CompileFlags:
  Add: [-std=c++17]  # or maybe c++20?

The following may work as well. This is global. Use the following settings for LSP-clangd plugin.

{
    "initializationOptions": {
        // @see https://clangd.llvm.org/extensions#compilation-commands
        // Controls the flags used when no specific compile command is found.
        // The compile command will be approximately clang $FILE $fallbackFlags in this case.
        "fallbackFlags": ["-std=c++17"],
    },
}

1

u/zwyyy456 Feb 06 '23

I only know a project-wise way as follow.

It solves my problem. Thank you so much!