Is there a way on VS Code to import Makefile proje

2020-06-16 06:11发布

问题:

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!

回答1:

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


回答2:

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!



回答3:

Visual Studio Code's Intellisense extension for C/C++ supports a compile_commands.json database. Some makefile utilities can generate these databases by themselves, but I don't have any experience using them to do so. With GNU make, you'll need some kind of helper script or tool. I used the python package compiledb:

sudo pip install compiledb
cd projectfolder
make clean
compiledb make

After running that, you should end up with a compile_commands.json file. Then, in VS Code, you need to edit c_cpp_properties.json for the workspace in question. One way to open it is by clicking the configuration name in the lower right corner of the status bar then clicking "Edit Configurations (json)". Or, you can open the command palatte (CTRL + SHIFT + P on my system), and entering C/C++: Edit configurations (JSON)

Then, in configuration you are using, add the property "compileCommands": "${workspaceFolder}/compile_commands.json"

Full example:

{
    "configurations": [
        {
            "name": "geany",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}/**"
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "compileCommands": "${workspaceFolder}/compile_commands.json"
        }
    ],
    "version": 4
}

Now, Visual Studio code's Intellisense analysis should be using the exactly correct include directories, define directives, and other command line compiler flags unique to each individual source file.