Godot 通过 GDExtension 配置 C++ 开发环境
本文介绍在 Godot 引擎中使用 GDExtension 配置 C++ 开发环境的流程。主流手段为 GDExtension,需配合官方维护的绑定库。
环境准备
安装 Visual Studio 和 Godot(建议使用最新 4.5 版本)。
获取绑定库
从 GitHub 下载官方维护的绑定库:godot-cpp。 参考官方文档说明:编译插件。
目录结构
创建主文件夹,包含以下结构:
- demo-cpp/:Godot 项目
- demo-cpp/bin:存放编译后的 C++ 扩展 DLL
- godot-cpp/:下载的 C++ 绑定库
- src/:编写的 C++ 扩展代码
配置 SCons
在根目录创建 SConstruct 文件。确保安装 SCons(可通过 Miniconda 管理 Python 环境)。
参考配置如下:
#!/usr/bin/env python
import os
import sys
env = SConscript("./godot-cpp/SConstruct")
env.Append(CPPPATH=["src/"])
sources = Glob("src/*.cpp")
if env["platform"] == "macos":
library = env.SharedLibrary(
"demo-cpp/bin/libgdexample.{}.{}.framework/libgdexample.{}".format(
env["platform"], env["target"], env["platform"], env["target"]
),
source=sources,
)
elif env["platform"] == "ios":
if env["ios_simulator"]:
library = env.StaticLibrary(
"demo-cpp/bin/libgdexample.{}.{}.simulator.a".format(env["platform"], env["target"]),
source=sources,
)
else:
library = env.StaticLibrary(
.(env[], env[]),
source=sources,
)
:
library = env.SharedLibrary(
.(env[], env[]),
source=sources,
)
Default(library)

