Linux系统VS Code配置CC++环境

Linux系统VS Code配置CC++环境,第1张

基础环境

系统:Ubuntu系统(本文使用Ubuntu 20.04)
确保系统已经有gcc,g++,gdb环境,可以使用下面命令查看:

gcc -v
g++ -v
gdb -v

正常结果举例:

~$ gdb -v
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later //gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

如果没有执行下面命令就可以了,会自动安装gcc、g++、gdb等:

sudo apt-get install build-essential
安装VS Code插件 VS Code常用插件

下面是一些vscode常用插件,以及c/c++环境一些必要插件。

  1. C/C++ // 必装,IntelliSense, debugging, and code browsing。提供C/C++支持
  2. Code Runner // 必装,提供编译后程序的运行环境
  3. C/C++ Snippets // 建议,提供一些常用的C/C++片段
  4. EPITECH C/C++ Headers // 建议,为C/C++文件添加头部(包括作者、创建和修改日期等),并为.h头文件添加防重复的宏
  5. File Templates // 建议,文件模板,可以自己添加文件模板
  6. GBKtoUTF8 // 建议,GBK编码文件转换为UTF-8
  7. Include Autocomplete // 建议,头文件自动补全
  8. One Dark Pro // 建议,可以打造好看的VS Code主题
如何安装这些插件

打开vscode,点击左边最下面的图标”扩展“,在应用商店中搜索扩展,把上面的插件名称都输入一遍,然后点击”安装“,就可以了。

比如Code Runner,点击安装,就可以了。

设置字体大小

刚刚安装好的vscode字体大小默认14,通常对我们来说偏小,如果你想设置字体大小可以这么做:
依次点击:文件->首选项->设置
更改Front Size,这里我的大小为22。
用户和工作区可以理解为,用户区的设置适用于该用户所 *** 作的所有文件,工作区的设置只适用于当前工作目录。

测试cpp文件

创建一个test.cpp文件

#include 

using namespace std;

int main() {
    cout << "hello world" << endl;
    return 0;
}


运行结果:

使用vscode调试c++程序

依次点击:运行和调试->运行和调试->C++(GDB/LLDB)-> C/C++: g++ 生成活动文件
或者直接按F5,再点击C++(GDB/LLDB)-> C/C++: g++ 生成活动文件。

断点成功显示:

这个时候查看文件,会发现多了task.json文件,launch.json还没有,我们可以自己在.vscode目录下创建一个launch.json。也可以F5->C++(GDB/LLDB)-> C/C++: cpp 生成活动文件。出现报错,直接点击打开launch.json,然后把下面的launch.json文件的内容复制到你的文件中,再找到test.cpp文件按F5,就可以直接调试了。
我的launch.json文件:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ 生成活动文件",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

我的task.json文件:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "调试器生成的任务。"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: cpp 生成活动文件",
            "command": "/usr/bin/cpp",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
运行程序相关

setting.json文件是关于运行时的一些设置,快捷键 ctl+shift+p,输入setting搜索。
点击:Preference:Open Settings(JSON),这是user用户区配置,对所有工作区适用。

我的setting.json文件:

{
    "editor.fontSize": 22,
    "workbench.colorTheme": "One Dark Pro",
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "compile",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
    "code-runner.runInTerminal": true,
    "debug.onTaskErrors": "showErrors",
    "C_Cpp.default.includePath": [
        
    ],
    "code-runner.executorMap": {

        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -std=c++11 && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    },
    "terminal.integrated.tabs.enabled": true,
    "C_Cpp.default.cppStandard": "c++20",
    "C_Cpp.default.cStandard": "c17",
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter-notebook"
    },
    "notebook.cellToolbarLocation": {
        "default": "right",
        "jupyter-notebook": "left"
    },
    "diffEditor.ignoreTrimWhitespace": false,
    "explorer.confirmDragAndDrop": false,
    "explorer.confirmDelete": false,
    
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/914900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存