我正在构建一个 Godot 扩展,这是我提交时的 Scons 文件 8907014:
#!/usr/bin/env python
import os
import sys
env = SConscript("godot-cpp/SConstruct")
# For reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags
llamacpp_path = ARGUMENTS.get('llamacpp_path', 'llama.cpp') # llama.cpp is a directory, not a cpp file :)
# Specify where to find headers and libraries
env.Append(CPPPATH=[
"src/",
llamacpp_path,
os.path.join(llamacpp_path, 'common'),
# "godot-cpp/include/godot_cpp/classes/" # Add this path for wrapped.hpp
])
sources = Glob("src/*.cpp")
# Include wrapped.cpp in your sources
# sources.append("godot-cpp/src/classes/wrapped.cpp")
# add 'pthread' to libraries
libraries = ['pthread']
env.Append(LIBS=libraries)
# Object files from the specified path
object_files = [
os.path.join(llamacpp_path, 'ggml-alloc.o'),
os.path.join(llamacpp_path, 'k_quants.o'),
os.path.join(llamacpp_path, 'ggml.o'),
os.path.join(llamacpp_path, 'common.o'),
os.path.join(llamacpp_path, 'llama.o')
]
# Create a static library from the object files
static_lib = env.StaticLibrary('llama', object_files)
# Link the shared library against the static library
if env["platform"] == "macos":
library = env.SharedLibrary(
"the-game/bin/libgdllm.{}.{}.framework/libgdllm.{}.{}".format(
env["platform"], env["target"], env["platform"], env["target"]
),
source=sources,
LIBS=[static_lib]
)
else:
library = env.SharedLibrary(
"the-game/bin/libgdllm{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
source=sources,
LIBS=[static_lib]
)
Default(library)
当 GDExtension 在引擎中加载时,会出现未定义的 godot-cpp 符号错误。在我添加 llama 静态库之前,这以前有效:
static_lib = env.StaticLibrary('llama', object_files)
如果没有 llama 静态库,我会得到未定义的 llama 符号。
我是 Scons 新手,所以在我看来,添加 llama 静态库破坏了 godot-cpp 环境。
鉴于 llama 和 godot-cpp 不相互依赖,这不是链接 order 问题。
我也尝试为godot-cpp和llama提供单独的环境,但之后我仍然得到未定义的godot-cpp符号。
回顾一下:
修剪掉,你就完成了:
library = env.SharedLibrary(
"the-game/bin/libgdllm{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
source=sources,
LIBS=[static_lib]
)
将关键字参数传递给构建器会创建 SCons 所谓的“覆盖环境” - 就像覆盖一样 - 优先于基本环境从覆盖中提取值。在覆盖中,您将
LIBS
设置为仅包含 static_lib
的列表,这意味着构建不会看到 LIBS
的任何原始值 - 换句话说,您掩盖了对 SConscript("godot-cpp/SConstruct")
的任何调用
设置 env["LIBS"]
(然后将 pthread
添加到其中)。您需要添加您的库(静态或共享应该没有区别),这有点棘手。一种方法可能是预先获取它,例如:
shliblist = env['LIBS'] + [static_lib]
library = env.SharedLibrary(... LIBS=shliblist ...)
补充意见:
# llama.cpp is a directory, not a cpp file :)
真的建议你不要这样做......SCons(或其他人)可能会误解这一点 - 事实上你需要该评论就是一个暗示。
# Specify where to find headers and libraries
env.Append(CPPPATH=[ ...
CPPPATH
仅适用于标题。如果要设置库搜索路径,则需要使用LIBPATH
(参见https://scons.org/doc/product/HTML/scons-man.html#cv-LIBPATH)