GDExtension clangd configuration

Godot Version

4.6.1 but not applicable

Does anyone have any experience configuring clangd for working on a gdextension that uses cmake ?

Running configure on the cmake project generates the compile_commands.json file as well as a .clangd file in the topmost project folder.

This all works to some extent, but clangd throws errors for some of the included content from the godot-cpp bindings.

I tried setting GODOTCPP_SYSTEM_HEADERS to on but it made no difference.

Any and all suggestions would be greatly appreciated. Note project builds and launches correctly.

Edit:
I get the same results if I use a vanilla version of the template and generate the compile_commands.json with scons.

Further Edit:

In trying to figure out the problem, i checked my gcc version:

gcc --version

Which returned version 14, however when I checked what version clang

clang++ -v

Which showed this:

Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/13
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/14
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/15
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/7
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/15

Once I installed the development package for gcc 15, most of the errors went away. It still shows an error on the #pragma once preprocesser directive but that isn’t that big of a deal.

Another Edit:
To fix the problem with the #pragma once directive I added this to the .clangd file under CompilerFlags like so:

Remove: [-fno-gnu-unique]

Is there a way to mark this post as the solution, or do I have to actually post the solution above in a new reply?

You will have to self-reply to mark as a solution, thank you for coming back with the solution too!

Does that mean the files couldnt be included twice?

#pragma once denotes that a file should only be included once, similar to header guards

#ifndef MY_HEADER
#define MY_HEADER

// your code here

#endif

is replaced by

#pragma once

// your code here

However this is a GCC extension and not officially C++, many compilers may not support it. So -fno-gnu-unique removes such extensions.

Yes it basically says to the compiler only add this header file once. It’s not a flag recognized by clang so adding the line to the CompilerOptions tells clang to ignore it.

Here’s the solution:

In trying to figure out the problem, i checked my gcc version:

gcc --version

Which returned version 14, however when I checked what version clang

clang++ -v

Which showed this:

Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/13
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/14
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/15
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/7
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/15

The fix was to install the development package for gcc 15.

In other words ensure that clang and your compiler are using the same version.

To fix the problem with the #pragma once directive I added this to the .clangd file under CompilerFlags like so:

Remove: [-fno-gnu-unique]
1 Like

Yeah i know, thats been in MSVC since 2007 or earlier, i was wondering if the file had to be included twice and not just once to get the extension build working :slight_smile: