Visual Studio 代码:C++ 包含路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36122358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:35:07  来源:igfitidea点击:

Visual Studio Code: C++ include path

c++visual-studio-codeinclude-path

提问by Deftness

I'm currently using https://marketplace.visualstudio.com/items?itemName=mitaki28.vscode-clangwhich is great as a nice little tool for accessing member functions.

我目前正在使用https://marketplace.visualstudio.com/items?itemName=mitaki28.vscode-clang,这是一个很好的访问成员函数的小工具。

I am however having one issue with a project I am importing. While the above clang feature works, I am having particular problem with using include directories. My project structure is as follows:

但是,我在导入的项目中遇到了一个问题。虽然上述 clang 功能有效,但我在使用包含目录时遇到了特殊问题。我的项目结构如下:

|- src/
   |- main.cpp
|- include/
   |- MyHelper.h
|- CMakeLists.txt

Is there a way to configure my include directories in Visual Studio Code such that in main.cppI can just do: #include "MyHelper.h"instead of #include "include/MyHelper.h"?

有没有办法在 Visual Studio Code 中配置我的包含目录,以便main.cpp我可以这样做: #include "MyHelper.h"而不是#include "include/MyHelper.h"?

In the editor, it highlights my include statement saying it's unable to find the file. While the editor is not a big deal (my project compiles), the subsequent issue is the vscode-clang plugin does not work because it does not see the file.

在编辑器中,它突出显示了我的包含声明,说它无法找到该文件。虽然编辑器不是什么大问题(我的项目编译),但随后的问题是 vscode-clang 插件不起作用,因为它看不到文件。

Perhaps even have it use the config from my CMakeLists.txt in the editor for necessary includes?

也许甚至让它在编辑器中使用我的 CMakeLists.txt 中的配置来进行必要的包含?

Thanks!

谢谢!

采纳答案by Deftness

Okay, this was foolish, but in the event someone uses Visual Studio Codeand does not have a trivial project. These instructions are assuming you're using clang compiler:

好吧,这是愚蠢的,但如果有人使用Visual Studio Code并且没有一个微不足道的项目。这些说明假设您正在使用 clang 编译器:

  1. Open your project directory
  2. Open .vscode/settings.json
  3. Configure the line below inside of the JSON object:

    // Compiler options for C++ (e.g. ['-std=c++11'])
    "clang.cxxflags": [
        "-I/path/to/my/include/directory" // header files
    ],
    
  1. 打开你的项目目录
  2. 打开 .vscode/settings.json
  3. 在 JSON 对象内部配置以下行:

    // Compiler options for C++ (e.g. ['-std=c++11'])
    "clang.cxxflags": [
        "-I/path/to/my/include/directory" // header files
    ],
    

回答by r0ng

If you are using CMake, VSCode has CMake plugins to help you build the project. So you do not need to modify the settings.json. Just use:

如果你使用 CMake,VSCode 有 CMake 插件来帮助你构建项目。所以你不需要修改settings.json。只需使用:

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") 

Or if there are no other modules used the header files in that folder you could use something like:

或者,如果没有其他模块使用该文件夹中的头文件,您可以使用以下内容:

target_include_directories(MyHelper, "${CMAKE_CURRENT_SOURCE_DIR}/include") 

If you only need the project be built successfully. That is the whole story.

如果您只需要成功构建项目。这就是整个故事。

In the case of that, you got some little green zigzag lines under the #include statements hurting your eyes. You need to generate c_cpp_properties.json. It has nothing to do with helping the compiler to build the code but for helping VSCode intellisense to realize the existence of libraries and header files. And again, you are able to leverage the CMake by adding CMake options in the CMakeLists.txt:

在这种情况下,#include 语句下的一些绿色小锯齿线会伤害您的眼睛。您需要生成 c_cpp_properties.json。它与帮助编译器构建代码无关,而是帮助 VSCode 智能感知实现库和头文件的存在。同样,您可以通过在 CMakeLists.txt 中添加 CMake 选项来利用 CMake:

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)

The CMake will generate a file compile_commands.jsonunder your build directory. The VSCode is able to parse the Json file and find the include path based on the content in that file. So what you need to do is just letting VSCode know where is the Json file. You can do that by adding follwing line in the c_cpp_properties.json:

CMake 将compile_commands.json在您的构建目录下生成一个文件。VSCode 能够解析 Json 文件并根据该文件中的内容找到包含路径。所以你需要做的只是让 VSCode 知道 Json 文件在哪里。您可以通过在 c_cpp_properties.json 中添加以下行来做到这一点:

 "configurations": [
        {
            "name": "Mac",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            ...
        }],

Rebuild the project will let the VSCode intellisense find all necessary paths.

重建项目会让 VSCode 智能感知找到所有必要的路径。

[Environment]
Ubuntu: 16.04.3
VSCode: 1.23.1
VSCode plugins: C/C++ 0.17.0, CMAKE 0.0.17, CMakeTools 0.11.1

[环境]
Ubuntu:16.04.3
VSCode:1.23.1
VSCode 插件:C/C++ 0.17.0、CMAKE 0.0.17、CMakeTools 0.11.1

回答by user11074381

I don't know if I'm late. I add arg in tasks.jsonfile. In fact, same as first answer, but in vscode, we can do it easier.

我不知道我是不是迟到了。我在tasks.json文件中添加了arg 。实际上,与第一个答案相同,但是在 vscode 中,我们可以做得更容易。

In C++, use g++ -g foo.cpp -o foo -I /path/to/include/dirto add headers files.

在 C++ 中,用于g++ -g foo.cpp -o foo -I /path/to/include/dir添加头文件。

As we know, in vscode, tasks.jsonis using to run bash command, but can use some alias like ${fileDirname}for, you know, file dir name:)

正如我们所知,在 vscode 中,tasks.json用于运行 bash 命令,但可以使用一些别名,例如${fileDirname}文件目录名:)

Anyway, task.json:

无论如何,task.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/out/${fileBasenameNoExtension}",
                "-I",
                "${fileDirname}/../Include/"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}