VS Code与Unreal Engine 5.2.1配置报错:无法打开Sailors.h
我是C++零基础新手,在Windows_NT x64 10.0.19044系统下配置Visual Studio Code(v1.79.2)与Unreal Engine 5.2.1。已完成工具下载、项目创建,也修改了c_cpp_properties.json添加头文件路径,但保存项目时出现以下错误:
#include errors detected. Consider updating your compile_commands.json or includePath. IntelliSense features for this translation unit (E:\My Work\Sailors\Source\Sailors\Private\Sailors.cpp) will be provided by the Tag Parser.
cannot open source file "Sailors.h"
我的c_cpp_properties.json配置如下:
{ "configurations": [ { "name": "SailorsEditor Editor Win64 Development (Sailors)", "compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools \\MSVC\\14.36.32532\\bin\\Hostx64\\x64\\cl.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "msvc-x64", "configurationProvider": "epic.ue", "compileCommands": "E:\\My Work\\Sailors\\.vscode\\compileCommands_Sailors.json", "includePath": [ "${workspaceFolder}\\Intermediate\\**", "${workspaceFolder}\\Plugins\\**", "${workspaceFolder}\\Source\\**", "E:\\Games\\Epic\\UE_5.2\\Engine\\Source\\Runtime\\Core\\Public" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE", "__UNREAL__", "UBT_COMPILED_PLATFORM=Windows", "WITH_ENGINE=1", "WITH_UNREAL_DEVELOPER_TOOLS=1", "WITH_APPLICATION_CORE=1", "WITH_COREUOBJECT=1" ] } ], "version": 4 }
已做尝试:
- 将C++(.cpp)存放在Private目录、头文件(.h)存放在Public目录
- 手动更新
compileCommands_Sailors.json - 尝试添加精确文件夹路径到
includePath - 新建空白UE项目测试
- 多次重启VSCode
解决方案
1. 显式添加模块Public目录到includePath
尽管${workspaceFolder}\\Source\\**理论上能覆盖所有子目录,但UE的模块结构(Source/[模块名]/Public/Private)需要更精确的路径优先级。在includePath中添加:
"${workspaceFolder}\\Source\\Sailors\\Public",
放在${workspaceFolder}\\Source\\**之前,确保IntelliSense优先查找该目录。
2. 解决配置冲突问题
你同时启用了UE官方插件的configurationProvider和手动配置的includePath/compileCommands,两者可能互相覆盖:
- 暂时注释掉
"configurationProvider": "epic.ue"这一行,保存配置后重启VSCode,检查IntelliSense是否恢复正常。 - 如果需要保留UE插件,打开
compileCommands_Sailors.json,确认其中Sailors.cpp对应的编译条目包含-I"E:\My Work\Sailors\Source\Sailors\Public"参数,没有的话重新生成配置。
3. 重新生成UE-VSCode配置文件
关闭VSCode,回到UE编辑器,点击Tools -> Refresh Visual Studio Code Project,让UE重新生成所有VSCode相关配置文件(包括compileCommands和c_cpp_properties.json),避免手动修改导致的配置不一致。
4. 检查头文件引用语法
确保Sailors.cpp中引用头文件的语法是#include "Sailors.h"(双引号),而非<Sailors.h>(尖括号)。双引号会让编译器优先在当前文件的上级目录(即Public)查找,符合UE的模块目录规范。
5. 重置IntelliSense缓存
打开VSCode,按Ctrl+Shift+P,输入并执行C/C++: Reset IntelliSense Database,然后重启VSCode,清除旧的缓存数据。
内容的提问来源于stack exchange,提问作者M K

