• windows下通过VS code编写编译调试c/c++程序


    都2020年了,你还在用dev c++编写编译c/c++程序吗兄弟??

    十五年前的怪病百出的东西还在win10上用?

    现在改用VS code吧!你的目标已经不是拿NOI rank1了!

    微软推出的VS code本身只有编辑器功能,并不自带c/c++编译器,所以需要自行安装c/c++编译器并在VScode中配置好。

    这里推荐采用minGW-64作为编译器,下载链接 https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/sjlj/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z/download

    下载后解压到自定义位置(也就相当于minGW-64的安装位置),然后添加到环境变量中。

    添加环境变量:

      系统属性.高级->环境变量->选择编辑系统变量中的“Path”,添加新条目,地址指向minGW-64的bin文件夹。->保存

    测试环境变量是否配置成功:

      按win+r运行cmd或者powershell输入gcc -v,如果返回正常则配置成功。

    接下来打开vs code,在插件库中搜索并安装下面两个插件

    ——C/C++

    ——Code Runner

    另外推荐几个插件也装上,用起来舒服些

    ——one dark pro

    ——bracket pair colorizer2

    ——vscode-icons

    ——live server

    到这里已经配置好编译功能了,接下来对调试功能进行配置。

    选择一个工作区,比如我选择"D:/vs_code_c"作为工作区,注意必须是英文路径奥

    然后在工作区下新建名为.vscode的文件夹,然后在文件夹中配置三个json文件,分别为:

    ——launch.json

    (注意将mingw64路径换为自己的mingw64路径)

    {
    
        "version": "0.2.0",
        
        "configurations": [
        
        {
        
        "name": "Run C/C++",
        
        "type": "cppdbg",
        
        "request": "launch",
        
        "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
        
        "args": [],
        
        "stopAtEntry": false,
        
        "cwd": "${workspaceFolder}",
        
        "environment": [],
        
        "externalConsole": true,
        
        "MIMode": "gdb",
        
        "miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe",
        
        "setupCommands": [
        
        {
        
        "description": "Enable pretty-printing for gdb",
        
        "text": "-enable-pretty-printing",
        
        "ignoreFailures": false
        
        }
        
        ],
        
        "preLaunchTask": "build & run file"
        
        },
        
        {
        
        "name": "Debug C/C++",
        
        "type": "cppdbg",
        
        "request": "launch",
        
        "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
        
        "args": [],
        
        "stopAtEntry": false,
        
        "cwd": "${workspaceFolder}",
        
        "environment": [],
        
        "externalConsole": true,
        
        "MIMode": "gdb",
        
        "miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe",
        
        "setupCommands": [
        
        {
        
        "description": "Enable pretty-printing for gdb",
        
        "text": "-enable-pretty-printing",
        
        "ignoreFailures": false
        
        }
        
        ],
        
        "preLaunchTask": "build & debug file"
        
        }
        
        ]
        
        }

    ——tasks.json

    {
    
        "version": "2.0.0",
        
        "tasks": [
        
        {
        
        "label": "build & debug file",
        
        "type": "shell",
        
        "command": "g++",
        
        "args": [
        
        "-g",
        
        "-o",
        
        "${fileBasenameNoExtension}",
        
        "${file}"
        
        ],
        
        "group": {
        
        "kind": "build",
        
        "isDefault": true
        
        }
        
        },
        
        {
        
        "label": "build & run file",
        
        "type": "shell",
        
        "command": "g++",
        
        "args": [
        
        "-o",
        
        "${fileBasenameNoExtension}",
        
        "${file}"
        
        ],
        
        "group": {
        
        "kind": "build",
        
        "isDefault": true
        
        }
        
        }
        
        ]
        
        }

    ——settings.json

    {
        "files.defaultLanguage": "c", // ctrl+N新建文件后默认的语言
        "editor.formatOnType": true,  // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
        "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
        "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
        // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
    
        "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
        "code-runner.executorMap": {
            "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
            "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
            // "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && $dir$fileNameWithoutExt",
            // "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && $dir$fileNameWithoutExt"
        }, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),PS和bash也能用,但文件名中有空格时无法运行
        "code-runner.saveFileBeforeRun": true, // run code前保存
        "code-runner.preserveFocus": true,     // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
        "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
        "code-runner.ignoreSelection": true,   // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
    
        "C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
    }

    设置好保存,重启vscode。

    file->打开工作区文件夹,新建一个cpp文件,不要建到.vscode文件夹里,写好程序后debug

    这样就ok啦

  • 相关阅读:
    Assert.isTrue 用法
    P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles
    最近目标2333
    LibreOJ β Round #2」贪心只能过样例
    CF1062F Upgrading Cities 拓扑排序
    CF1108F MST Unification
    CF915D Almost Acyclic Graph 拓扑排序
    Swift日历控件Calendar
    README.md的markdown语法
    MAC打开App显示已损坏
  • 原文地址:https://www.cnblogs.com/dynmi/p/12230892.html
Copyright © 2020-2023  润新知