Is there a way on VS Code to import Makefile projects?

Andrea Nisticò picture Andrea Nisticò · Dec 7, 2018 · Viewed 25.6k times · Source

As the title said, can I populate automatically c_cpp_properties.json from an existing Makefile?

Edit:

For others trying to import makefiles, I have found a set of scripts that perform exactly what I wanted to achieve, the management of STM32 embedded projects through VS Code. If you dig into the scripts folder you can find the make parser and the VSCode project files populator.

Here the repo, enjoy!

Answer

Reinier Torenbeek picture Reinier Torenbeek · Dec 8, 2018

This answer may not entirely give you what you want, but hopefully it helps you setting up your VS Code environment.

In your question title, you mention "Makefile projects", which is an indication that you have the wrong impression that (GNU) Makefiles capture project settings in a way similar to Visual Studio project files do. Makefiles do not work like that and the short answer to your question is No, there does not seem to be a way to "import" Makefiles into VS Code and have it automatically set values in your c_cpp_properties.json file.

Having said that, there are some other mechanisms that may help you getting started with VS Code in your situation, depending on the other elements you have in your toolchain.

VS Code works with the premise that it opens one or more directories in your file system. When you do so, it will automatically scan all its subdirectories, display the tree and do linting (if enabled) to detect problems. Indeed, include directories need to be added manually to your c_cpp_properties.json file but to add all your subdirectories recursively to the list of include directories, you can use the expression

"${workspaceRoot}/**"

as one of the elements in your includePath setting. See the blog post Visual Studio Code C/C++ extension May 2018 Update – IntelliSense configuration just got so much easier! for more details. That often resolves a lot of the missing symbols. If you are using external or 3rd party libraries as well, then you have to add those to your includePath setting manually, there is no way around that.

Additionally, you can create VS Code Tasks to execute your make command to your liking. This is explained here: Integrate with External Tools via Tasks. The Microsoft C/C++ Extension comes with a set of so-called problemMatchers that parse the output of your command and can interpret that for certain known compilers. You will see hyperlinks for errors and warning to navigate directly to the associated location in your code.

An example of such a Task may look like this:

    {
        "label": "Build All Debug",
        "type": "shell",
        "command": "make -f path/to/Makefile DEBUG=1",
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "reveal": "always",
            "panel": "new"
        },
        "problemMatcher": [
            "$gcc"
        ]
    }