2

This question has been asked before, but it seems none of the answers apply to my files. I'm using clangd 15.0.7 in VSCode on Windows 11 and macOS 13.2. Please note that gcc 11.3.0, the actual compiler for this project, doesn't emit any warnings. Here's the code :

minmax.h

#ifndef _KPPV_MATH_H_
#define _KPPV_MATH_H_

unsigned int min( unsigned int left, unsigned int right ); /* ISO C requires a translation unit to contain at least one declarationclang(-Wempty-translation-unit) */

unsigned int max( unsigned int left, unsigned int right );

#endif

minmax.c

#include "minmax.h"

unsigned int min( unsigned int left, unsigned int right ) {
    return left < right ? left : right;
}

unsigned int max( unsigned int left, unsigned int right ) {
    return left > right ? left : right;
}

.clangd

CompileFlags:
    Add: [-W, -Wall, -pedantic, -xc, -std=c89, ]
    Remove: [-Wempty-translation-unit]

( The Remove thing obviously doesn't work because of -pedantic -Wall but I don't want people to suggest that, so I included it to show it doesn't work )

ice-wind
  • 690
  • 4
  • 20
  • 1
    Why use a code guard named `_KPPV_MATH_H_` for a header file named `minmax.h`? I suspect the code guard it not acting as expected - perhaps it (or `min()`) defined elsewhere? – chux - Reinstate Monica Mar 26 '23 at 14:53
  • @chux-ReinstateMonica While the guard is indeed weird, changing it doesn't do anything (I checked). I might try renaming the functions though ! – ice-wind Mar 26 '23 at 15:07
  • Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says: — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/q/1449181) – Jonathan Leffler Mar 26 '23 at 17:27
  • Do you mean that you added `-Wno-empty-translation-unit` yet the error still appeared? What exactly was the command that was executed? Also, why are you compiling to a standard that's over 30 years old and has 3 successor versions (and a fourth due later this year or early next year)? – Jonathan Leffler Mar 26 '23 at 17:30

1 Answers1

1

The issue stems from the -xc flag in your compile flags. This flags means not only "C language", but more specifically, "a C language source file" as opposed to "a C language header file". Trying to apply this flag to header files will cause some misbehaviour.

There is instead an -xc-header flag that you can apply to header files.

Example .clangd file that implements this adjustment:

# Flags for all files
CompileFlags:
    Add: [-W, -Wall, -pedantic, -std=c89]
    Remove: [-Wempty-translation-unit]

---

# Flags for .h files
If:
  PathMatch: .*\.h

CompileFlags:
  Add: [-xc-header]

---

# Flags for .c files
If:
  PathMatch: .*\.c

CompileFlags:
  Add: [-xc]

UPDATE Wanted to mention one other thing: the Add: and Remove: sections under CompileFlags: specify edits to make to the compile command.

So, Remove: [-Wempty-translation-unit] will only have an effect if the incoming command (from compile_commands.json for example) already contains -Wempty-translation-unit.

Disabling a warning can be accomplished by adding a flag to disable it, e.g.:

CompileFlags:
  Add: [-Wno-empty-translation-unit]

That would also work (but I recommend the -xc-header approach for better overall results).

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • It works ! Thank you Also, I knew there was some way to disable warnings but for some reason my searches didn't turn anything up ? Either I'm bad at looking things up or it's duckduckgo's fault – ice-wind Mar 29 '23 at 10:59