猿问
下载APP

如何设置Visual Studio代码来编译C ++代码?

Microsoft的Visual Studio代码编辑器相当不错,但它没有构建C ++项目的默认支持。


如何配置它来执行此操作?


米脂
浏览 64回答 3
3回答

蛊毒传说

有一种更简单的方法来编译和运行C ++代码,无需配置:安装Code Runner Extension在文本编辑器中打开C ++代码文件,然后使用快捷方式Ctrl+Alt+N,或者按F1,然后选择/键入Run Code,或者右键单击文本编辑器,然后Run Code在上下文菜单中单击,代码将被编译并运行,输出将显示在输出窗口。此外,您可以根据需要使用不同的C ++编译器更新settings.json中的配置,C ++的默认配置如下:"code-runner.executorMap": {    "cpp": "g++ $fullFileName && ./a.out"}

慕勒3428872

构建任务是特定于项目的。要创建新项目,请在Visual Studio代码中打开目录。按照此处的说明操作,按Ctrl+ Shift+ P,键入Configure Tasks,选择它并按Enter。tasks.json文件将被打开。将以下构建脚本粘贴到文件中,然后保存:{    "version": "0.1.0",    "command": "make",    "isShellCommand": true,    "tasks": [        {            "taskName": "Makefile",            // Make this the default build command.            "isBuildCommand": true,            // Show the output window only if unrecognized errors occur.            "showOutput": "always",            // Pass 'all' as the build target            "args": ["all"],            // Use the standard less compilation problem matcher.            "problemMatcher": {                "owner": "cpp",                "fileLocation": ["relative", "${workspaceRoot}"],                "pattern": {                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",                    "file": 1,                    "line": 2,                    "column": 3,                    "severity": 4,                    "message": 5                }            }        }    ]}现在转到菜单文件 → 首选项 → 键盘快捷键,并为构建任务添加以下键绑定:// Place your key bindings in this file to overwrite the defaults[    { "key": "f8",          "command": "workbench.action.tasks.build" }]现在当您按下F8Makefile时将执行,并且编辑器中的错误将加下划线。

浮云间

新的2.0.0 tasks.json版本的makefile任务示例。在下面的一些评论的片段中,我希望它们会有用。{&nbsp; &nbsp; "version": "2.0.0",&nbsp; &nbsp; "tasks": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "label": "<TASK_NAME>",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type": "shell",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "command": "make",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // use options.cwd property if the Makefile is not in the project root ${workspaceRoot} dir&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "options": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "cwd": "${workspaceRoot}/<DIR_WITH_MAKEFILE>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // start the build without prompting for task selection, use "group": "build" otherwise&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "group": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "kind": "build",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "isDefault": true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "presentation": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "echo": true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "reveal": "always",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "focus": false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "panel": "shared"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // arg passing example: in this case is executed make QUIET=0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "args": ["QUIET=0"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use the standard less compilation problem matcher.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "problemMatcher": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "owner": "cpp",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "fileLocation": ["absolute"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "pattern": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "file": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line": 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "column": 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "severity": 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "message": 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]}
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答