找不到Linux Visual Studio Code C ++文件

时间:2018-10-23 03:21:19

标签: c++ visual-studio-code

Visual Studio代码的新手。我已经建立了一个基本的opencv项目,并试图使其进行编译。我已经在c_cpp_properties.json中设置了我的包含路径,并正确获取了自动补全功能。但是,当我去编译g ++时,它并不知道包含的目录。解决此问题的最佳方法是什么?我的假设是我还需要将其传递给tasks.json,但是我不确定为包含目录做到这一点的好方法。

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4"
            ],
            "defines": ["_DEBUG"],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build hello world"
        }
    ]
}

显示图像的基本代码

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, const char** argv)
{ 
    Mat image;
    image = imread("./images/test.jpg");

    if(!image.data)
    {
        std::cout << "No image data" << std::endl;
        return -1;
    }

    namedWindow("Display Image",WINDOW_AUTOSIZE);
    imshow("Display Image", image);

    waitKey(0);

    return 0;
}

错误

> Executing task: g++ -g main.cpp <

main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
 #include <opencv2/opencv.hpp>
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

1 个答案:

答案 0 :(得分:1)

c_cpp_properties.json仅是IntelliSense的配置。您必须在task.json中单独配置所需的编译器参数(包括dirs)。

tasks.json

+---------------------------------------------+
| ID, Studios, City, State, Open              |
+---------------------------------------------+
| '1', 'T', 'B', 'I', '0'                     |
| '2', 'C', 'C', 'O', '0'                     |
| '3', 'R', 'W', 'D', '0'                     |
+---------------------------------------------+