3

Recently I wanted to use a compile_flags.txt-file to allow development in C++20. Clangd shows annoying warnings when using structured-bindings and C++-related features and so I created this file. My compile_flags.txt-file looks like this:

-std=c++20

When I want to edit a C-file however, clangd complains for the basic include directive: #include <stdio.h>:

clang[drv_argument_not_allowed_with]: Invalid argument '-std=c++20' not allowed with 'C'.

Naturally, I removed the compile_flags.txt-file and it works fine. How can I achieve writing C++20-code and C-code at the same time without manually removing the file every time I decide to switch between the two? Thanks for any help in advance :^)

  • 2
    Have your build system write a `compile_commands.json` with the correct flags for each file. `compile_flags.txt` assumes that each file is compiled in the same way. – user17732522 May 18 '23 at 13:22
  • @user17732522 Oh ok. Could you provide an example (maybe as an answer) on how such a file might look like? – Eldinur the Kolibri May 18 '23 at 13:26
  • You shouldn't need to write it yourself. Your build system should do it for you. But you didn't mention what build system you are using. All the details, including a link to the specification of the `compile_commands.json` file can be found in the documentation for clangd: https://clangd.llvm.org/installation.html – user17732522 May 18 '23 at 13:31
  • @user17732522 At the moment I don't use a build system at all. That's why I didn't specify one. But if it's in the documentation, I guess I will use CMake to generate it for me and see what it does. – Eldinur the Kolibri May 18 '23 at 13:33
  • @user17732522 I generated the file just now and my conclusion is: I need to use a build system for it to work correctly. Is that necessarily the case? Or does a workaround exist somehow? – Eldinur the Kolibri May 18 '23 at 13:47
  • I question how you manage to develop with multiple files and different compiler invocations without any kind of build system. You should at least have something as simply as a shell script or Makefile. As the linked documentation mentions, you can then usually use `bear` (regardless of the details of the build system) to generate the database. If you are using some editor/IDE plugin for clangd, it may also support specifying the flags (again, see linked doc). – user17732522 May 18 '23 at 13:52

1 Answers1

1

If you don't want to use a compile_commands.json file, another mechanism for providing flags to clangd that is more flexible than compile_flags.txt is a clangd config file.

Clangd config files can specify compiler flags to be added, and you can limit your configuration to only files whose path matches a certain pattern (for example, files with a certain extension). This allows specifying different configuration for different languages based on the file extension.

Here is an example of a config file that specifies that the flag -std=c++20 should be added for .cpp files, and the flag -std=c17 should be added for .c files:

If:
  PathMatch: .*\.cpp

CompileFlags:
  Add: [-std=c++20]

---

If:
  PathMatch: .*\.c

CompileFlags:
  Add: [-std=c17]

This would go into a file named .clangd in your project's root directory.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194