The Summator example in Custom modules in C++ is great for small,
custom modules, but what if you want to use a larger, external library?
Let’s look at an example using Festival, a speech synthesis (text-to-speech)
library written in C++.

To bind to an external library, set up a module directory similar to the Summator example:

/* tts.cpp */#include"tts.h"#include<festival.h>boolTTS::say_text(Stringp_txt){//convert Godot String to Godot CharString to C stringreturnfestival_say_text(p_txt.ascii().get_data());}voidTTS::_bind_methods(){ClassDB::bind_method(D_METHOD("say_text","txt"),&TTS::say_text);}TTS::TTS(){festival_initialize(true,210000);//not the best way to do it as this should only ever be called once.}

Just as before, the new class needs to be registered somehow, so two more files
need to be created:

register_types.h
register_types.cpp

With the following contents:

/* register_types.h */voidregister_tts_types();voidunregister_tts_types();/* yes, the word in the middle must be the same as the module folder name */

/* register_types.cpp */#include"register_types.h"#include"core/class_db.h"#include"tts.h"voidregister_tts_types(){ClassDB::register_class<TTS>();}voidunregister_tts_types(){// Nothing to do here in this example.}

Next, you need to create a SCsub file so the build system compiles
this module:

# SCsubImport('env')env_tts=env.Clone()env_tts.add_source_files(env.modules_sources,"*.cpp")# Add all cpp files to the build

You’ll need to install the external library on your machine to get the .a library files. See the library’s official
documentation for specific instructions on how to do this for your operation system. We’ve included the
installation commands for Linux below, for reference.

Important: The voices that Festival uses (and any other potential external/3rd-party
resource) all have varying licenses and terms of use; some (if not most) of them may be
be problematic with Godot, even if the Festival Library itself is MIT License compatible.
Please be sure to check the licenses and terms of use.

The external library will also need to be installed inside your module to make the source
files accessible to the compiler, while also keeping the module code self-contained. The
festival and speech_tools libraries can be installed from the modules/tts/ directory via
git using the following commands:

Important: Please note that Git submodules are not used in the Godot repository. If
you are developing a module to be merged into the main Godot repository, you should not
use submodules. If your module doesn’t get merged in, you can always try to implement
the external library as a GDNative C++ plugin.

To add include directories for the compiler to look at you can append it to the
environment’s paths:

env_tts.Append(CPPPATH=["speech_tools/include","festival/src/include"])# this is a path relative to /modules/tts/# http://www.cstr.ed.ac.uk/projects/festival/manual/festival_28.html#SEC132 <-- Festival library documentationenv_tts.Append(LIBPATH=['libpath'])# this is a path relative to /modules/tts/ where your .a library files reside# You should check with the documentation of the external library to see which library files should be included/linkedenv_tts.Append(LIBS=['Festival','estools','estbase','eststring'])

If you want to add custom compiler flags when building your module, you need to clone
env first, so it won’t add those flags to whole Godot build (which can cause errors).
Example SCsub with custom flags:

# SCsubImport('env')env_tts=env.Clone()env_tts.add_source_files(env.modules_sources,"*.cpp")env_tts.Append(CCFLAGS=['-O2'])# Flags for C and C++ codeenv_tts.Append(CXXFLAGS=['-std=c++11'])# Flags for C++ code only