GDextension Include Third Party Lib in Scons

Godot Version

4.61

I did the godotcon2024 tutorial for creating a gd extension. This was easy enough.

Now I want to start playing with using a third party lib as part of an extension. The third party library requires cmake to build. Adding this in the cmake file for the extension was simple enough.

Edit:

Turns out cmake wasn’t working properly either as soon as I tried to use the library I ended up with errors, multiple times it prints x is
only available with ‘-std=c++20’ or ‘-std=gnu++20’

Before adding the code to the extension I had did a poc and the folder layout was the same as it is now, with same cmake commands and it builds fine. Any suggestions? Yes I did add this to my CMakeLists.txt:
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED YES)

Figured out why this happening somewhere the following flag is set:
-std=gnu++17 does anyone know where I can change this flag?

Further Edit:
Figured out how to set the flag to use c++ 20, adding set_property(TARGET ${LIBNAME} PROPERTY CXX_STANDARD 20) to CMakeLists.txt seems to have worked. But now I have a new error and need to find a different flag. The library uses iostreams which require exceptions to be enabled, and I can’t find where any of the flags are set, to enable exceptions. If I set the flags in the extension’s CMakeLists.txt the settings are overidden when the cmake file for godot-cpp is called. I see in the cmake file for godot-cpp that there are two calls to methods compiler_detection() adn godotcpp_generate and I assume this is where my compiler flags are being overridden, any ideas?

However I want to make the build process agnostic and have already setup the necessary build targets for running scons and launching the extension. In order to set up scons I was checking out:

However I notice it is extremely out of date, and much of what it refers to does not mesh with the current template, and the summator example is very different then the one presented in the tutorial. At the top of the pages there is a link saying check this page in the stable branch for additions and corrections but the page is unavailable.

So I have two questions, what do I have to add to SConstruct to use cmake to build the third party lib or build it itself, and then include it as a link target. The src folder for the library is in the top level (lsp-framework) in the screenshot.

Second question is, does anyone have any links to more up to date documentation for creating gdextensions?

since you are consuming a library that needs to be built with cmake, why not build your extension using cmake? godotengine/godot-cpp-template is a good starting point.

I am using the template and I can’t get it to work with CMake either.

First I couldn’t find where to set the flag to get it to use c++ 20, when I managed to solve that I immediately ran into another problem.

The library uses iostreams so I need to exception support. Currently no matter what I put in my cmake file I cannot change it to compile using exceptions because when the godot-cpp make file is called, it overrides any settings and uses compiler flags from somewhere that I cannot find.

They are not in the CMakeLists.txt file in the godot-cpp folder. In order to get it to compile with cmake I have to find where the compiler flags are set so I can remove -fno-exceptions and replace it with -fexceptions I can currently append the -fexceptions to the cxx flags however because it is appended it comes after -fno-exceptions and is therefore ignored.

There is an option for enabling exceptions, GODOTCPP_DISABLE_EXCEPTIONS=NO which mirrors the scons option. you can see the user facing options for cmake with -LH or something. I cant remember off the top of my head but any cached variables should show up there.

Strange that this should come up twice in the same day, but with opposite experiences, the other user was reporting that exceptions were enabled by default.

The options are in the godot-cpp/cmake/godot-cpp.cmake file at the top. and generic ones are added to the compiler commands in the common.cmake file. platform specific ones are added in their respective platform.cmake files.

I’m available on discord and dev chat if you need further help.

How do I use this? I added it as :
option(GODOTCPP_DISABLE_EXCEPTIONS=NO)

To the cmake file for the extension and it causes compilation errors.

CMake Error at CMakeLists.txt:3 (option): option called with incorrect number of arguments: GODOTCPP_DISABLE_EXCEPTIONS=NO

there are two ways i know of. either specify it on the command line, or in a tool.

or in your cmake files before including godot-cpp add it cached using the set command like

set( GODOTCPP_DISABLE_EXCEPTIONS CACHE "DOCSTRING" OFF)

Check the syntax of the set command as i might have the order slightly out.
if it were on the command line, it would be something like:

cmake -S <source_dir> -B <build_dir> -DGODOTCPP_DISABLE_EXCEPTIONS:BOOL=NO

Or something very similar to that.

Because the variable is cached, make sure that the cache is updated after you make your changes, because it can be a pain when you think you’ve changed something but the cached value is still the old one.

Afaik scons should be able to invoke cmake. It’d require implementing a custom Builder object in sconstruct though. I haven’t used this myself and can’t find any direct boilerplate but asking some chatbot may give you a workable recipe.

I’d be happy to get either working. Or even consitency, as today I opened the tutorial project to play with and even though it was working fine yesterday, today it won’t build using cmake, instead I get the following error:

Cannot generate into /home/jordan/Code/CPP/gd_one/godot-cpp-template
It was created with incompatible generator 'Unix Makefiles'

Please either delete it manually or select another generation directory

And I haven’t changed anything since yesterday when it was working. It still builds and runs through scons, but cmake no longer works. And it’s just the simple Summator and Traffic Light examples.

How would I set the same enable exceptions setting in Scons?

Edit:

Started a new project, added
option(GODOTCPP_DISABLE_EXCEPTIONS OFF)
to both cmake files, the extension’s and the one in godot-cpp.
Note I also tried NO in place of OFF , but cmake -LH says:

// Force disabling exception handling code (ON|OFF)
GODOTCPP_DISABLE_EXCEPTIONS:BOOL=OFF

But when building I still get the same error, cmake runs fine but I still get the same eror.
error: exception handling disabled, use ‘-fexceptions’ to enable

And sure enough looking the compiler flags it is still setting the flag as -fno-exceptions

Further Edit:
I Tried:
set( GODOTCPP_DISABLE_EXCEPTIONS CACHE "DOCSTRING" OFF)

After clearing the cmake cache it broke everything I now get :

home/jordan/Code/CPP/godot5/godot-cpp-template
It was created with incompatible generator 'Unix Makefiles'

Please either delete it manually or select another generation directory

Edit:

Started yet another project and this time got a little farther.
set( GODOTCPP_DISABLE_EXCEPTIONS OFF)

seemed to work for enabling exceptions, however things don’t link properly. I had worked with the lib in question, using same project folder layout with wxwidgets to test before trying in the extension and that worked without much effort.


FAILED: testClient.linux.template_debug.x86_64.so 
: && /usr/bin/c++ -fPIC -g  -shared -Wl,--dependency-file=CMakeFiles/testClient.dir/link.d -Wl,-soname,testClient.linux.template_debug.x86_64.so -o testClient.linux.template_debug.x86_64.so CMakeFiles/testClient.dir/src/Summator.cpp.o CMakeFiles/testClient.dir/src/lspClient.cpp.o CMakeFiles/testClient.dir/src/register_types.cpp.o CMakeFiles/testClient.dir/src/example_class.cpp.o CMakeFiles/testClient.dir/gen/doc_source.cpp.o  lsp-framework/liblspd.a  bin/libgodot-cpp.linux.template_debug.x86_64.a && cd /home/jordan/Code/CPP/godot6/godot-cpp-template/cmake-build-debug && /home/jordan/bin/clion-2025.3.3/bin/cmake/linux/x64/bin/cmake -E copy /home/jordan/Code/CPP/godot6/godot-cpp-template/cmake-build-debug/testClient.linux.template_debug.x86_64.so /home/jordan/Code/CPP/godot6/godot-cpp-template/project/bin/linux/testClient.linux.template_debug.x86_64.so
/usr/lib64/gcc/x86_64-suse-linux/14/../../../../x86_64-suse-linux/bin/ld: lsp-framework/liblspd.a(connection.cpp.o): relocation R_X86_64_32 against symbol `_ZNSt6ranges5equalE' can not be used when making a shared object; recompile with -fPIC
/usr/lib64/gcc/x86_64-suse-linux/14/../../../../x86_64-suse-linux/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Okay figured out link error changed lib to use -fPIC and it now builds at least.

Thanks for the help. Also if anyone knows how I can enable exceptions in the SConstruct file that would be nice.

discussion in the dev channel had someone post this localEnv["disable_exceptions"] = False

dont know where exactly that should go, but it might help

Thanks.

Been trying to see if it works. But I can’t get scons to build things even with the lib pre-built and trying to link to the .a files as in the docs sample, but no joy so far.

It seems like even though it’s supposed to be linking to the library it seems to think the import statements are file paths as it raises errors like
src/lspClient.h:7:10: fatal error: lsp/connection.h: No such file or directory

While in cmake it builds and runs, for what little it does at this point.