How to prepare/configure development environment for C++ projects in Visual Code Editor?

戏子无情 提交于 2019-12-20 03:19:25

问题


I'm working with JavaScript projects using nodejs and visual code editor. I wonder is it possible to configure such a great code editor for C++ projects.

I want to link the debugger and make some hotkeys for building the debug/release versions of project.

Is it possible for C++ projects and what should I do/read for it?


回答1:


I want to link the debugger

This is currently not possible until there is a public extension API available. I expect it to come in November or December this year.

I want to [...] make some hotkeys for building the debug/release versions of project.

You can do it right now if there is only one project you want to compile in your workspace. This is how to do it:

  • Open the root folder of your project in VSCode (this is your workspace)
  • Place a batch/shell script in the workspace that accepts a parameter with a value of release/debug and compiles the project in release or debug mode depending on the passed parameter value
  • In case there is no .vscode directory in the workspace then create it on your own
  • Add a file tasks.json to that folder having this content:

    {
      "version": "0.1.0",
      "command": "${workspaceRoot}/CompileProject.bat",
      "tasks": [
         {
              "taskName": "Compile debug build",
              "args": [
                "debug" 
              ],
              "isTestCommand": true            
         },
         {
              "taskName": "Compile release build",
              "args": [
                "release" 
              ],
              "isBuildCommand": true            
         }         
      ]
    }
    

You can trigger Compile debug build with CTRL + Shift + T and Compile release build with CTRL + Shift + B.

You can change the keybindings by going to File -> Preferences -> Keyboard Shortcuts and define your preferred shortcuts for the commands workbench.action.tasks.test and workbench.action.tasks.build. Example:

[
    { "key": "f5",          "command": "workbench.action.tasks.test" },
    { "key": "f6",          "command": "workbench.action.tasks.build" } 
]



回答2:


Use the following in the tasks.json file, changing "helloworld" strings as appropriate.

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process

{
    "version": "0.1.0",
    "command": "gcc",
    "args": ["-Wall", "helloWorld.c", "-o", "helloWorld"],
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

EDIT: This requires gcc to be available on the path. The build can be triggered with Ctrl + shift + b. Debugger is not available yet AFAIK



来源:https://stackoverflow.com/questions/33098442/how-to-prepare-configure-development-environment-for-c-projects-in-visual-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!