天天看點

VSCode如何快速搭建C/C++環境1、前言2、下載下傳3、安裝4、設定.vscode5、下載下傳Code Runner 插件6、運作代碼7、問題

1、前言

說明下如何在VSCode下面搭建C/C++環境以及運作

2、下載下傳

點選該連結,進行ming64安裝包下載下傳:

VSCode安裝請自行百度,這裡不在贅述。

3、安裝

1、将下載下傳完成後的安裝包,解壓放到C槽下面即可,如下圖所示:

VSCode如何快速搭建C/C++環境1、前言2、下載下傳3、安裝4、設定.vscode5、下載下傳Code Runner 插件6、運作代碼7、問題

2、添加環境變量

在我的電腦上點選右鍵-->屬性:進入環境變量配置C:\mingw64\bin; (要與mingw64安裝位置相對應)

VSCode如何快速搭建C/C++環境1、前言2、下載下傳3、安裝4、設定.vscode5、下載下傳Code Runner 插件6、運作代碼7、問題

4、設定.vscode

在對應的工程目錄下面建立.vscode檔案,并且對應建立如下三個檔案

VSCode如何快速搭建C/C++環境1、前言2、下載下傳3、安裝4、設定.vscode5、下載下傳Code Runner 插件6、運作代碼7、問題

c_cpp_properties.json

将下面内容直接複制到該檔案下面即可

{
    "configurations": [
        {
            //win32 作業系統名字
            "name": "Win32",
            // 提供.h檔案的搜尋目錄
            "includePath": [
                "C:/mingw64/include",
                "C:/mingw64/x86_64-w64-mingw32/include",
                //根據的安裝路徑更改
                "${workspaceRoot}/**"
            ],
            //編譯時加入的宏定義
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "compilerPath": "C:\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14"
        }
    ],
    "version": 4
}
           

launch.json

将下面内容直接複制到該檔案下面即可

{
    // 懸停以檢視現有屬性的描述。
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++",
            "type": "cppdbg",
            "request": "launch",
            "targetArchitecture": "x64",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "miDebuggerPath": "C:/mingw64/bin/gdb.exe",
            "MIMode": "gdb",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "internalConsoleOptions": "openOnFirstSessionStart",
            "externalConsole": false,
            "preLaunchTask": "gcc"
        }
    ]
}
           

settings.json

将下面内容直接複制到該檔案下面即可

{
    "files.associations": {
        "stdarg.h": "c",
        "utils.h": "c",
        "string.h": "c",
        "typedef.h": "c",
        "stdlib.h": "c",
        "stdio.h": "c",
        "gd32f4xx_usart.h": "c",
        "gd32f4xx.h": "c",
        "limits": "c",
        "cmath": "cpp",
        "*.tcc": "cpp",
        "cstdlib": "cpp"
    },
    // 這裡可以設定代碼界面風格
    "C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, ColumnLimit: 150, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Linux, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false}",
    "code-runner.executorMap": {
        "c": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    },
}
           

5、下載下傳Code Runner 插件

打開VSCode, 進入應用商店,輸入Code Runner 進行插件下載下傳安裝即可

VSCode如何快速搭建C/C++環境1、前言2、下載下傳3、安裝4、設定.vscode5、下載下傳Code Runner 插件6、運作代碼7、問題

6、運作代碼

接下來在對應的工程下面 建立一個main檔案,輸入代碼後,在main.c檔案裡點選右鍵選擇RunCode 運作代碼即可

VSCode如何快速搭建C/C++環境1、前言2、下載下傳3、安裝4、設定.vscode5、下載下傳Code Runner 插件6、運作代碼7、問題

7、問題

整理了遇到過的問題

VSCode個人常用配置

使用者區的

{
    "files.autoGuessEncoding": true,
    "files.trimTrailingWhitespace": false,
    "files.autoSave": "afterDelay",
    "files.encoding": "gb2312",
    "scm.alwaysShowActions": true,
    "debug.inlineValues": "on",
    "workbench.colorTheme": "Monokai",
    "extensions.closeExtensionDetailsOnViewChange": true,
    "extensions.ignoreRecommendations": true,
    "update.mode": "manual",
    "update.showReleaseNotes": false,
    "explorer.confirmDelete": false,
    "explorer.confirmDragAndDrop": false,
    "security.workspace.trust.untrustedFiles": "open",
    "cnblogsClientForVSCode.workspace": "e:\\other\\wei-ting",
    "git.path": "C:\\soft\\Git\\Git\\cmd\\git.exe",
    "git.ignoreMissingGitWarning": true,
    "git.enableSmartCommit": true,
    "git.ignoreLimitWarning": true,
    "git.ignoreLegacyWarning": true,
    "git.confirmSync": false,
    "git.autoStash": true,
    "git.alwaysShowStagedChangesResourceGroup": true,
    "editor.renderControlCharacters": true,
    "editor.renderWhitespace": "all",
    "editor.tabSize": 4,
    "editor.fontSize": 16,
    "editor.detectIndentation": false,
    "editor.autoClosingBrackets": "beforeWhitespace",
    "editor.dragAndDrop": false,
    "editor.wordWrapColumn": 180,
    "editor.largeFileOptimizations": false,
    "editor.formatOnSave": false,
    "diffEditor.ignoreTrimWhitespace": false,
    "typescript.format.enable": false,
    "typescript.format.placeOpenBraceOnNewLineForControlBlocks": false,
    "typescript.format.placeOpenBraceOnNewLineForFunctions": false,
    "C_Cpp.clang_format_style": "{ BasedOnStyle: Google, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Linux, ColumnLimit: 150, AlignConsecutiveMacros: true,}",
    "C_Cpp.clang_format_sortIncludes": false,
    "C_Cpp.dimInactiveRegions": true,
    "code-runner.runInTerminal": true,
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.ignoreSelection": true,
    "code-runner.showRunIconInEditorTitleMenu": true,
    "code-runner.showRunCommandInEditorContextMenu": true,
    "code-runner.respectShebang": true,
    "code-runner.executorMap": {
        "c": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "python": "python"
    },
    "code-runner.executorMapByFileExtension": {
        ".vb": "cd $dir && vbc /nologo $fileName && $dir$fileNameWithoutExt",
        ".vbs": "cscript //Nologo",
        ".scala": "scala",
        ".jl": "julia",
        ".cr": "crystal",
        ".ml": "ocaml",
        ".exs": "elixir",
        ".hx": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        ".rkt": "racket",
        ".ahk": "autohotkey",
        ".au3": "autoit3",
        ".kt": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
        ".kts": "kotlinc -script",
        ".dart": "dart",
        ".pas": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        ".pp": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        ".d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        ".hs": "runhaskell",
        ".nim": "nim compile --verbosity:0 --hints:off --run",
        ".csproj": "dotnet run --project",
        ".fsproj": "dotnet run --project"
    },
    "code-runner.languageIdToFileExtensionMap": {
        "bat": ".bat",
        "powershell": ".ps1",
        "typescript": ".ts"
    },
    "[c]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "[cpp]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "workbench.editorAssociations": {
        "*.exe": "default"
    },
    "editor.suggestSelection": "first",
    "workbench.startupEditor": "none",
    "markdown-preview-enhanced.previewTheme": "vue.css",
    "explorer.excludeGitIgnore": true,
}
 
           

繼續閱讀