Chase R Lewis
Chase R Lewis

Reputation: 2307

Linux Visual Studio Code C++ File Not Found

New to visual studio code. I've setup a basic opencv project and am trying to get it to compile. I've setup my include path in the c_cpp_properties.json and get auto-completion correctly. However, when I go to compile g++ says it doesn't know of the included directory. What is the best way to fix this issue? My assumption is I need to also pass it to the tasks.json, but i'm unsure of a good way to do that for include directories.

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"
        }
    ]
}

Basic code to show an image

#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;
}

Error

> 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.

Upvotes: 2

Views: 1431

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38773

c_cpp_properties.json is the config for IntelliSense only. You have to configure required compiler arguments (include dirs) in tasks.json separately.

tasks.json

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

Upvotes: 2

Related Questions