Menu Share

GDExtension CMake Programmer Setup

by | Published on
category CMake

There has been a week since Godot 4 set sail. I got some very good feedback on the first article that I wrote on how to script using GDExtension. So in this part 2 of GDExtension I will show you how to automate the CMake development a bit and wrap it up in a single place.

Table of Contents


In my first article the setup was simple and I wrote that you need to copy some shared library files from your built C++ project to your Godot project directory. Here I will show you how to make this setup a bit more automatic so that you don’t have to move those files. There is a small limitation though and it is that shared libraries cannot be replaces while the engine is running. This is a bigger downside to programming in a low-level language like C++. It is more appropriate for studios where programmers are separate from developers and you have no problem with longer programming-to-feedback cycle. Otherwise it is still better to go with GDScript instead of GDExtension.

Now just in case – check out the first article to get familiar with the original setup that we did in CMake and you can get started with this one. I have also written similar automation article for Godot 3’s GDNative system.

Project Structure

What we had last time in CMake was this simple project structure:

  • gameplay
    • src
      • extension_registration.cpp
      • movement.h
      • movement.cpp
    • CMakeLists.txt
  • CMakeLists.txt

The goal here is to move the godot project in a folder that is relative to the root of the project. So when we create the project in godot we can set it to use a folder like this:

  • gdproject
  • gameplay
  • CMakeLists.txt

This way in our CMakeLists.txt that is in the root of the project we can create some references to the gdproject directory.

The Root CMakeLists.txt

We only need to do one single thing in our root CMakeLists.txt file to make the gdextension running again for us. This is to add the following custom target:

# ... previous code

add_custom_target(create_link ALL
    COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_SOURCE_DIR}/gdproject/gameplay/"
    COMMAND "${CMAKE_COMMAND}" -E rm -rRf "${CMAKE_SOURCE_DIR}/gdproject/gameplay/bin/"
    COMMAND "${CMAKE_COMMAND}" -E create_symlink "$<TARGET_FILE_DIR:gameplay>" "${CMAKE_SOURCE_DIR}/gdproject/gameplay/bin/"
    COMMENT "Creating symbolic link to build directory"
    DEPENDS gameplay

This custom target will create a folder link between the output directory of the gameplay target build and the godot directory where the library should be located. Now you can make this even more advanced to make it work with mutliple platforms and things like that. This minimal setup will allow anybody to easily compile for GDExtension locally.

You can learn how to code these few lines yourself if you start your CMake journey. Check out my course on the topic if you’re more interested into this.


I have also created a simple template in github where you can check out this setup for yourself and even start from it or propose modifications if you’re more exprienced.

Leave a comment

Your email address will not be published. Required fields are marked *