【问题标题】:VSCode include path property not working properlyVSCode 包含路径属性无法正常工作
【发布时间】:2021-11-07 22:05:27
【问题描述】:

背景故事

  • 我正在制作一个用于自学的小型游戏引擎项目/我正在使用 Vulkan 图形 API 和 GLFW,我使用 CMake 编译它。在编译时一切正常,但是在 VSCode 中编写代码时,它会给我错误的错误提示 cannot open source file "GLFW/glfw3.h"C/C++(1696)。即使我继续编译并将其包含到主文件中,它也可以按预期工作。

  • 我已尝试重新启动 VSCode、重新启动计算机、重建 CMake 和删除缓存,尝试使用 <>"" 进行包含,并使用 VSCode 建议将其再次包含在 CPP 属性文件中。

  • 重要的一点是,如果我将相同的代码包含到我的 main.cpp 文件中,一切正常,但是在我将代码分成单独的文件并尝试包含 GLFW 后,问题就开始出现了。

文件结构:

+-> .vscode
| |
| +-> c_cpp_properties.json
| +-> settings.json
|
+-> bin
| |
| +-> Abyssal.exe
|
+-> build (CMake build files)
|
+-> src
| |
| +-> AbyssalWindow.h
| +-> main.cpp
|
+-> vendor (nothing inside this folder)
+-> CMakeLists.txt 
{
    "env": {
        "myDefaultIncludePath": [
            "${workspaceFolder}",
            "${workspaceFolder}/src"
        ]
    },
    "configurations": [
        {
            "name": "Windows",
            "intelliSenseMode": "${default}",
            "includePath": [
                "${myDefaultIncludePath}",
                "C:/VulkanSDK/1.2.189.0/Include",
                "C:/glfw-3.3.4/include"
            ],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

【问题讨论】:

    标签: c++ visual-studio-code cmake include


    【解决方案1】:

    您说您想使用 CMake 作为您的构建系统。我强烈建议您将所有构建设置保留在 CMake 中。

    要转换您的 vscode 配置,请使用:

    set(CMAKE_C_STANDARD 17)
    set(CMAKE_CXX_STANDARD 17)
    
    # declare global include directories used by all targets
    # by using 'SYSTEM' many compilers will hide internal warnings
    include_directories(SYSTEM
      "C:/VulkanSDK/1.2.189.0/Include"
      "C:/glfw-3.3.4/include"
    )
    

    从您的 vscode 配置中删除相应的设置,并将您新创建的头文件添加到您对 add_executable 的调用中,以便 vscode 获取该文件的正确包含目录。

    您可能更喜欢 include_directories 而不是

    target_include_directories(YOURTARGET ...)
    

    更具体/“现代”。

    【讨论】:

    • 我实际上已经在 CMake 文件中拥有了我所有的构建系统。我遇到的问题不是构建文件,甚至不是链接器问题,它实际上只是智能感知,包括来自 GLFW 的头文件。
    猜你喜欢
    • 2019-12-18
    • 2015-07-20
    • 2016-03-20
    • 2016-10-03
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多