执行scons platform=windows generate_bindings=yes时触发路径不存在错误
GDNative C++编译路径错误解决
问题描述
我正在学习GDNative C++教程,在gdtest/godot-cpp目录下执行命令:
scons platform=windows generate_bindings=yes
触发如下错误:
C:\Users\Antonio>cd gdtest C:\Users\Antonio\gdtest>cd godot-cpp C:\Users\Antonio\gdtest\godot-cpp>scons platform=windows generate_bindings=yes scons: Reading SConscript files ... scons: *** Path for option target_path does not exist: demo/bin/ File "C:\Users\Antonio\gdtest\godot-cpp\SConstruct.py", line 26, in <module>
现有文件夹路径:
C:\Users\Antonio\gdtest\demo\bin C:\Users\Antonio\gdtest\godot-cpp C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin C:\Users\Antonio\gdtest C:\msys64\mingw64\bin
SConstruct文件内容:
import os opts = Variables([], ARGUMENTS) # Gets the standard flags CC, CCX, etc. env = DefaultEnvironment() # Define our options opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release'])) opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'linuxbsd', 'linux', 'osx'])) opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", '', ['', 'windows', 'linuxbsd', 'linux', 'osx'])) opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no')) opts.Add(PathVariable('target_path', 'The path where the lib is installed.', 'demo/bin/')) opts.Add(PathVariable('target_name', 'The library name.', 'libgdexample', PathVariable.PathAccept)) # Local dependency paths, adapt them to your setup godot_headers_path = "godot-cpp/godot-headers/" cpp_bindings_path = "godot-cpp/" cpp_library = "libgodot-cpp" # only support 64 at this time.. bits = 64 # Updates the environment with the option variables. opts.Update(env) # Process some arguments if env['use_llvm']: env['CC'] = 'clang' env['CXX'] = 'clang++' if env['p'] != '': env['platform'] = env['p'] if env['platform'] == '': print("No valid target platform selected.") quit() # For the 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 # Check our platform specifics if env['platform'] == "osx": env['target_path'] += 'osx/' cpp_library += '.osx' env.Append(CCFLAGS=['-arch', 'x86_64']) env.Append(CXXFLAGS=['-std=c++17']) env.Append(LINKFLAGS=['-arch', 'x86_64']) if env['target'] in ('debug', 'd'): env.Append(CCFLAGS=['-g', '-O2']) else: env.Append(CCFLAGS=['-g', '-O3']) elif env['platform'] in ('linuxbsd', 'linux'): env['target_path'] += 'linuxbsd/' cpp_library += '.linux' env.Append(CCFLAGS=['-fPIC']) env.Append(CXXFLAGS=['-std=c++17']) if env['target'] in ('debug', 'd'): env.Append(CCFLAGS=['-g3', '-Og']) else: env.Append(CCFLAGS=['-g', '-O3']) elif env['platform'] == "windows": env['target_path'] += 'win64/' cpp_library += '.windows' # This makes sure to keep the session environment variables on windows, # that way you can run scons in a vs 2017 prompt and it will find all the required tools env.Append(ENV=os.environ) env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS']) env.Append(CCFLAGS=['-W3', '-GR']) env.Append(CXXFLAGS='/std:c++17') if env['target'] in ('debug', 'd'): env.Append(CPPDEFINES=['_DEBUG']) env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI']) env.Append(LINKFLAGS=['-DEBUG']) else: env.Append(CPPDEFINES=['NDEBUG']) env.Append(CCFLAGS=['-O2', '-EHsc', '-MD']) if env['target'] in ('debug', 'd'): cpp_library += '.debug' else: cpp_library += '.release' cpp_library += '.' + str(bits) # make sure our binding library is properly includes env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/']) env.Append(LIBPATH=[cpp_bindings_path + 'bin/']) env.Append(LIBS=[cpp_library]) # tweak this if you want to use different folders, or more folders, to store your source code in. env.Append(CPPPATH=['src/']) sources = Glob('src/*.cpp') library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources) Default(library) # Generates help for the -h scons option. Help(opts.GenerateHelpText(env))
已安装VS 2022,求解决该路径错误问题。
解决方案
1. 修正相对路径指向
错误核心是SConstruct中target_path的默认值为demo/bin/,但当前执行目录是gdtest/godot-cpp,相对路径会指向gdtest/godot-cpp/demo/bin/,而实际存在的路径是gdtest/demo/bin/。
修改SConstruct中的对应行:
# 原代码 opts.Add(PathVariable('target_path', 'The path where the lib is installed.', 'demo/bin/')) # 修改为 opts.Add(PathVariable('target_path', 'The path where the lib is installed.', '../demo/bin/'))
2. 自动创建目标子目录
编译Windows平台时,代码会自动在target_path后追加win64/,即最终路径为../demo/bin/win64/,需要确保该目录存在。可以手动创建,或者在SConstruct中添加自动创建逻辑:
在opts.Update(env)之后添加:
# 确保目标路径存在 import os if not os.path.exists(env['target_path']): os.makedirs(env['target_path'])
3. 验证VS环境变量加载
确保执行scons时是在VS 2022开发者命令提示符中运行,这样才能正确加载编译器所需的环境变量,避免后续编译出现工具链缺失问题。
内容的提问来源于stack exchange,提问作者Antoner8
相关产品推荐
相关产品推荐

