简体   繁体   English

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

[英]Visual Studio Code: C++ include path

I'm currently using https://marketplace.visualstudio.com/items?itemName=mitaki28.vscode-clang which 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.虽然上述 clang 功能有效,但我在使用包含目录时遇到了特殊问题。 My project structure is as follows:我的项目结构如下:

|- 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.cpp I 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!谢谢!

If you are using CMake, VSCode has CMake plugins to help you build the project.如果你使用 CMake,VSCode 有 CMake 插件来帮助你构建项目。 So you do not need to modify the settings.json.所以你不需要修改settings.json。 Just use:只需使用:

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.在这种情况下,#include 语句下的一些绿色小锯齿线会伤害您的眼睛。 You need to generate c_cpp_properties.json.您需要生成 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.它与帮助编译器构建代码无关,而是帮助 VSCode 智能感知实现库和头文件的存在。 And again, you are able to leverage the CMake by adding CMake options in the CMakeLists.txt:同样,您可以通过在 CMakeLists.txt 中添加 CMake 选项来利用 CMake:

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)

The CMake will generate a file compile_commands.json under your build directory. CMake 将在您的构建目录下生成一个文件compile_commands.json The VSCode is able to parse the Json file and find the include path based on the content in that file. VSCode 能够解析 Json 文件并根据该文件中的内容找到包含路径。 So what you need to do is just letting VSCode know where is the Json file.所以你需要做的只是让 VSCode 知道 Json 文件在哪里。 You can do that by adding follwing line in the c_cpp_properties.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 Ubuntu:16.04.3
VSCode: 1.23.1 VSCode:1.23.1
VSCode plugins: C/C++ 0.17.0, CMAKE 0.0.17, CMakeTools 0.11.1 VSCode 插件:C/C++ 0.17.0、CMAKE 0.0.17、CMakeTools 0.11.1

Okay, this was foolish, but in the event someone uses Visual Studio Code and does not have a trivial project.好吧,这是愚蠢的,但如果有人使用Visual Studio Code并且没有一个简单的项目。 These instructions are assuming you're using clang compiler:这些说明假设您正在使用 clang 编译器:

  1. Open your project directory打开你的项目目录
  2. Open .vscode/settings.json打开.vscode/settings.json
  3. Configure the line below inside of the JSON object:在 JSON 对象内部配置以下行:

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

I don't know if I'm late.我不知道我是不是迟到了。 I add arg in tasks.json file.我在tasks.json文件中添加了 arg。 In fact, same as first answer, but in vscode, we can do it easier.实际上,与第一个答案相同,但是在 vscode 中,我们可以做得更容易。

In C++, use g++ -g foo.cpp -o foo -I /path/to/include/dir to add headers files.在 C++ 中,使用g++ -g foo.cpp -o foo -I /path/to/include/dir添加头文件。

As we know, in vscode, tasks.json is 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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM