scons 找不到 #include 文件

问题描述

我正在学习关于设置 gdnative 的 this 教程。

我已经通过 Visual Studio、python 3.2 和 scons 4.1.0 安装了 C++ 工具

我一直试图让 scons 构建 this gdnative 示例。我遇到的问题似乎是 scons 无法找到 #include 文件。我曾尝试使用到子目录 godot-cpp/ 的相对文件路径、到同级目录 ../godot-cpp/ 的相对路径,以及使用到同级目录 E:/Projects/Godot Projects/Units/godot-cpp/ 的完整路径,但我得到了相同的结果每次都出错。我提供了一个屏幕截图来显示文件结构。我正在运行来自 X64 Native Tools Command Prompt for VS 2019

的 scons

编辑 - 现在也尝试以管理员身份运行,同样的错误

左上角:我正在尝试构建的项目以及我需要包含在同级目录中的 cpp 文件

右上角:项目文件夹的内容godot-cpp/ 子目录是左上角同名文件夹的副本。

左下:godot-cpp/内容

右下:godot-cpp/godot-headers内容。突出显示文件是它似乎找不到的文件

File structure

子目录的相对路径:

E:\Projects\Godot Projects\Units\gdnative_cpp_example>scons platform=windows
scons: Reading sconscript files ...
scons: done reading sconscript files.
scons: Building targets ...
cl /Fosrc\gdexample.obj /c src\gdexample.cpp /TP /nologo -DWIN32 -D_WIN32 -D_WINDOWS -W3 -GR -D_CRT_SECURE_NO_WARNINGS -EHsc -D_DEBUG -MDd /I. /Igodot-cpp\godot_headers /Igodot-cpp\include /Igodot-cpp\include\core /Igodot-cpp\include\gen /Isrc
gdexample.cpp
godot-cpp\include\core\Godot.hpp(7): Fatal error C1083: Cannot open include file: 'gdnative_api_struct.gen.h': No such file or directory
scons: *** [src\gdexample.obj] Error 2
scons: building terminated because of errors.

兄弟目录的相对路径:

E:\Projects\Godot Projects\Units\gdnative_cpp_example>scons platform=windows
scons: Reading sconscript files ...
scons: done reading sconscript files.
scons: Building targets ...
cl /Fosrc\gdexample.obj /c src\gdexample.cpp /TP /nologo -DWIN32 -D_WIN32 -D_WINDOWS -W3 -GR -D_CRT_SECURE_NO_WARNINGS -EHsc -D_DEBUG -MDd /I. "/IE:\Projects\Godot Projects\Units\godot-cpp\godot_headers" "/IE:\Projects\Godot Projects\Units\godot-cpp\include" "/IE:\Projects\Godot Projects\Units\godot-cpp\include\core" "/IE:\Projects\Godot Projects\Units\godot-cpp\include\gen" /Isrc
gdexample.cpp
E:\Projects\Godot Projects\Units\godot-cpp\include\core\Godot.hpp(7): Fatal error C1083: Cannot open include file: 'gdnative_api_struct.gen.h': No such file or directory
scons: *** [src\gdexample.obj] Error 2
scons: building terminated because of errors.

兄弟目录的完整路径:

E:\Projects\Godot Projects\Units\gdnative_cpp_example>scons platform=windows
scons: Reading sconscript files ...
scons: done reading sconscript files.
scons: Building targets ...
cl /Fosrc\gdexample.obj /c src\gdexample.cpp /TP /nologo -DWIN32 -D_WIN32 -D_WINDOWS -W3 -GR -D_CRT_SECURE_NO_WARNINGS -EHsc -D_DEBUG -MDd /I. "/IE:\Projects\Godot Projects\Units\godot-cpp\godot_headers" "/IE:\Projects\Godot Projects\Units\godot-cpp\include" "/IE:\Projects\Godot Projects\Units\godot-cpp\include\core" "/IE:\Projects\Godot Projects\Units\godot-cpp\include\gen" /Isrc
gdexample.cpp
E:\Projects\Godot Projects\Units\godot-cpp\include\core\Godot.hpp(7): Fatal error C1083: Cannot open include file: 'gdnative_api_struct.gen.h': No such file or directory
scons: *** [src\gdexample.obj] Error 2
scons: building terminated because of errors.

它在从 Godot.hpp line 7gdnative_api_struct.gen.h 的包含语句之后卡住了。看起来它可以很好地解析路径,但由于某种原因无法打开它们。

SConstruct 文件 - 随我要构建的项目一起提供。唯一的修改是将路径更改为 godot-cpp/godot-cpp/headers/

#!python
import os,subprocess

opts = Variables([],ARGUMENTS)

# Gets the standard flags CC,CCX,etc.
env = DefaultEnvironment()

# Define our options
opts.Add(EnumVariable('target',"Compilation target",'debug',['d','r','release']))
opts.Add(EnumVariable('platform',"Compilation platform",'',['','windows','x11','linux','osx']))
opts.Add(EnumVariable('p',"Compilation target,alias for 'platform'",'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
# These next two lines are where I changed the paths       <-----------------
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();

# Check our platform specifics

# I'm using windows so I cut the other options for readability

if 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(CCFLAGS = ['-DWIN32','-D_WIN32','-D_WINDOWS','-W3','-GR','-D_CRT_SECURE_NO_WARNINGS'])
    if env['target'] in ('debug','d'):
        env.Append(CCFLAGS = ['-EHsc','-D_DEBUG','-MDd'])
    else:
        env.Append(CCFLAGS = ['-O2','-EHsc','-DNDEBUG','-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))

解决方法

我今天遇到了完全相同的问题。在您在 SConstruct 文件中更改的行中,您将 godot 头目录(包含 gdnative_api_struct.gen.h 的目录)引用为 godot_headers,但在您的文件系统中,此文件夹称为 godot-headers ({{ 1}} 不是 -)。由于这个 scons 无法找到该文件。将文件系统中的目录名称更改为 _ 为我解决了问题。