clangd / coc-clangd

clangd extension for coc.nvim
Apache License 2.0
750 stars 34 forks source link

Can I set clangd works only with standard from c++17 #20

Closed stokhos closed 4 years ago

stokhos commented 4 years ago

Hi,

Is there a way to set clangd only work with c++ 17 standard? best

fannheyward commented 4 years ago

~Try set "clangd.arguments": ["-std=c++17"] to your coc-settings.json.~

Use "clangd.fallbackFlags": [ "-std=c++17" ].

stokhos commented 4 years ago

How can I do this? Do I need to unninstall coc-clangd? below is my coc-settings.json { "languageserver": { "clangd": { "command": "clangd", "args": ["--background-index", "-std=c++17"], "rootPatterns": ["compile_flags.txt", "compile_commands.json"], "filetypes": ["c", "cpp"] }, } this works, but is there a way of not deleting coc-clangd and add "clangd.arguments": ["-std=c++17"] to your coc-settings.json at same time?

hokein commented 4 years ago

To clarify, the flag -std=c++17 is not passed to the clangd binary argument, it needs to be added in the compilation command. If you use the compile_flags.txt, just put it there.

is there a way of not deleting coc-clangd and add "clangd.arguments": ["-std=c++17"] to your coc-settings.json at same time?

Yes, if you use coc-clangd, the coc-settings.json is like below (not other fields)

{
   "clangd.arguments": [...]
}
stokhos commented 4 years ago

It works. Thanks guys!

treeshateorcs commented 4 years ago

i did as you said in the second comment in this thread and i now get

[coc.nvim] The "clangd" server crashed 5 times in the last 3 minutes. The server will not be restarted.

P.S. putting it into compile_flags.txt works tho, but i would rather not do that every time i start a new project

kadircet commented 4 years ago

that's WAI, as indicated in Haojian's comment

To clarify, the flag -std=c++17 is not passed to the clangd binary argument, it needs to be added in the compilation command. If you use the compile_flags.txt, just put it there.

if you pass it to clangd, command line parsing will fail and clangd will exit with a non-zero error code.

Waqar144 commented 4 years ago

Yes, if you use coc-clangd, the coc-settings.json is like below (not other fields)

{
   "clangd.arguments": [...]
}

Doing this make clangd crash very quickly. Not recommended anymore.

I can't get C++17 support, so many errors. It isn't able to find string_view even. Can anyone suggest something else?

sam-mccall commented 4 years ago

Doing this make clangd crash very quickly

Well, are you putting clangd flags in there? -std=c++17 is not a clangd flag, it must go in your compile_commands.json or compile_flags.txt instead. (clangd will not crash in this case, but it won't start correctly either).

Incidentally, if you really want to set this globally for all projects, the next release (clangd 11) will make this possible.

mixolydianmel commented 3 years ago

I used the clangd.fallbackFlags option and it worked fine for me. This is my coc-settings.json:

{
    "clangd.fallbackFlags": [ "-std=c++17" ]
}
Waqar144 commented 3 years ago

I have found the compile_flags.txt method to be the best. However I still have the problem where clangd thinks .h files are C header files so it keeps complaining

kadircet commented 3 years ago

you can also put -xc++-header into your compile_flags.txt

zhiwayzhang commented 2 years ago

I used the clangd.fallbackFlags option and it worked fine for me. This is my coc-settings.json:

{
    "clangd.fallbackFlags": [ "-std=c++17" ]
}

It's works! It's take me hole day to setting this config, every solution from internet didn't work.😭

lijh8 commented 2 years ago

I used the clangd.fallbackFlags option and it worked fine for me. This is my coc-settings.json:

{
    "clangd.fallbackFlags": [ "-std=c++17" ]
}

I'm trying C++ in VSCode with clangd. I need this:

    "clangd.fallbackFlags": [
        "-std=c++2a",
    ],

to turn off the error messages in problem pane in VSCode, e.g.:

No member named 'exchange' in namespace 'std'

It has to be in "clangd.fallbackFlags". If I put it in "clangd.arguments", the outline view does not work.

MuhammadSawalhy commented 1 year ago

I opened the output of coc-clangd to see why this solution is not working for me and this is what I got:

:CocCommand workspace.showOutput clangd
clangd: Unknown command line argument '-std=c++17'.  Try: '/usr/bin/clangd --help'
clangd: Did you mean '--j=c++17'?
fannheyward commented 1 year ago

@MuhammadSawalhy what's your clangd version?

MuhammadSawalhy commented 1 year ago

@MuhammadSawalhy what's your clangd version?

I switched to clangd.fallbackFlags instead of clangd.arguments and it works now. Thanks!

MuhammadSawalhy commented 1 year ago

This doesn't work with C files! How to use clangd.fallbackFlags with cpp files only?

image

Waqar144 commented 1 year ago

Read the following link carefully, then use compile commands to solve your problems.

https://clangd.llvm.org/installation.html

lijh8 commented 1 year ago

// workspace settings: //.vscode/settings.json "clangd.fallbackFlags": [ //"-std=c99", "-I../foo", ],

for C files, I comment the -std option

MuhammadSawalhy commented 1 year ago

// workspace settings:

I am coding separate and independent C++ and C files in the same project. This is my repo which contains solutions for problems from sites like LeetCode, Hackerrank, and Codeforces.

So I need a way to configure -std flag differently for C++ and C files.

Leskodamus commented 1 year ago

// workspace settings:

I am coding separate and independent C++ and C files in the same project. This is my repo which contains solutions for problems from sites like LeetCode, Hackerrank, and Codeforces.

So I need a way to configure -std flag differently for C++ and C files.

@MuhammadSawalhy Have you found a solution for having the -std flag separately for C and C++ files?