VSCode intellisense with C++ headers
Asked Answered
C

1

8

I have searched for this but I can't find anything. If it's dupe I will close my question without any problem. I have a c_cpp_properties.json configuration file in VSCODE

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/usr/include",
                "/usr/local/include",
                "${workspaceRoot}"
            ],
            "defines": [],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "/usr/include",
                    "/usr/local/include",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        },
        {
            "name": "Linux",
            "includePath": [
                "/usr/include/x86_64-linux-gnu/c++/5",
                "/usr/include/c++/5",
                "/usr/local/include",
                "/usr/include/x86_64-linux-gnu",
                "/usr/include",
                "${workspaceRoot}"
            ],
            "defines": [],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "/usr/include/x86_64-linux-gnu/c++/5",
                    "/usr/include/c++/5",
                    "/usr/local/include",
                    "/usr/include/x86_64-linux-gnu",
                    "/usr/include",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        },
        {
            "name": "Win32",
            "includePath": [
                "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
                "${workspaceRoot}"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 2
}

I'm developing in Ubuntu 16.04. The problem that I'm facing is that when I type in my .cpp files intellisense for headers are not working.

MyFooClass.h
#pragma once

#include <cstddef>
#include <fstream>
#include <string>

class MyFooClass
{
private:
    //My private fields

public:
    MyFooClass();
    virtual ~MyFooClass();
    bool MyFooFunction();
};

When I implement MyFooClass.cpp using

#include "MyFooClass.h"

Intellisense is not working for functions and data in .h It seems to me that this should be enabled in by default in my configuration but I dont know if I must add something new. Thank you very much.

Chastise answered 10/8, 2017 at 11:28 Comment(0)
H
8

In case you are still interested or anyone stumbles across this thread using Google:

VSC has to two different engines for auto-completion. 1. The legacy "Tag Parser" 2. The IntelliSense engine

The latter is the default one at this point, "Tag Parser" is a fallback solution. As you assumed, both are configured in c_cpp_properties.json. The paths in browse are searched recursively and used by Tag Parser only, whereas the paths in includePath are NOT searched recursively and used by the IntelliSense engine.

Given that your header MyFooClass.h is not located directly at the root folder but in a subfolder include, you would have to add "${workspaceRoot}/include" to your includePath in order to have a working IntelliSense code completion.

Nowadays they have a better documentation on this: https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/LanguageServer/c_cpp_properties.json.md

Hispanicism answered 17/3, 2018 at 17:49 Comment(1)
this is interesting but do you have any solution to the problem? I am used to put header files directly to the cpp files (not a library builder), i.e. in the same folder, so that the relative path should work fine. Do I need to add ALL subfolders into includePath now (despite using ** which should do recursive search but doesn't?)Mcentire

© 2022 - 2024 — McMap. All rights reserved.