Art by takepon
558 字
3 分钟
(水)使用你喜欢的任何编译系统编译 GDExtension(在 Godot 使用 C++)
Godot 的文档在 GDExtension 部分的开头是这样说的(中间的加粗是我加的,原文没有):
这里有一些前置需求是你需要的:
A Godot 4 executable.
A C++ compiler.
使用 Scons 作为构建工具。
A copy of the godot-cpp repository.
但是我们真的必须用 Scons 作为构建工具构建你自己的 GDExtension 吗?当然不。下面是你需要做的事情:
- 新建一个 GDExtension。(同文档)
- 添加头文件路径。
- 添加库搜索路径。
- 链接 godot-cpp 的构建产物。
- 指定输出名字等必要信息。
新建一个 GDExtension
我的做法是使用git submodule添加godot-cpp到我的项目下:
mkdir godot-cpp-test && cd godot-cpp-testgit initgit submodule add -b 4.5 https://github.com/godotengine/godot-cpp然后让 godot 生成extension_api.json:
godot --dump-extension-api然后编译godot-cpp:
cd godot-cppscons platform=<platform> custom_api_file=../extension_api.jsoncd ..接下来新建你的源代码文件,我这里直接用了lib.cpp作为名字。你的可能有多个,那些都无伤大雅。
emacs lib.cpp添加头文件路径
对于 GCC 和 Clang,使用-I选项来指定头文件路径。你需要添加以下几个目录:
godot-cpp/gen/includegodot-cpp/includegodot-cpp/gdextension
例子:
g++ lib.c -o demo/bin/libgdexample -shared -fPIC -Igodot-cpp/gen/include -Igodot-cpp/include -Igodot-cpp/gdextensionxmake 的写法:
add_rules("mode.debug", "mode.release")
target("gdexample") set_kind("shared") add_files("lib.cpp") add_includedirs("godot-cpp/gen/include") add_includedirs("godot-cpp/include")添加库搜索路径
对于 GCC 和 Clang,使用-L选项来指定库文件搜索路径,因为你需要链接godot-cpp/bin/libgodot-cppxxx.xxx.xxx.so,即godot-cpp的产物。
你需要添加的目录是:godot-cpp/bin
例子:
g++ lib.c -o demo/bin/libgdexample -shared -fPIC -Igodot-cpp/gen/include -Igodot-cpp/include -Igodot-cpp/gdextension -Lgodot-cpp/binxmake:
...target("gdexample") ... add_linkdirs("godot-cpp/bin")链接 godot-cpp 的构建产物
对于 GCC 和 Clang,使用-l选项来指定库文件名字
Scons 生成的文件名会带上平台等信息。所以你需要自行拼接文件名。我这里直接把它写死在脚本里了:
g++ lib.c -o demo/bin/libgdexample -shared -fPIC -Igodot-cpp/gen/include -Igodot-cpp/include -Igodot-cpp/gdextension -Lgodot-cpp/bin -llibgodot-cpp.linux.template_debug.x86_64.axmake:
...target("gdexample") ... add_linkdirs("godot-cpp/bin") add_links("libgodot-cpp.linux.template_debug.x86_64.a")指定输出名字等必要信息。
我推荐直接生成到 Godot 项目路径的bin目录下,节省手动复制或剪切文件的时间。
至于文件名。我推荐和 Scons 保持一致(也就是说你需要自己拼接包含平台等信息的文件名),否则你在下一步可能会陷入文件名不够用的窘境。
g++ lib.c -o demo/bin/libgdexample.linux.template_debug.x86_64.so -shared -fPIC -Igodot-cpp/gen/include -Igodot-cpp/include -Igodot-cpp/gdextension -Lgodot-cpp/bin -llibgodot-cpp.linux.template_debug.x86_64.axmake:
add_rules("mode.debug", "mode.release")
set_targetdir("demo/bin")
target("gdexample") ... set_filename("libgdexample.linux.template_debug.x86_64.so")部分信息可能已经过时