vscode配置C/C++笔记(硬要使用C++11)

  • 1 launch.json 控制的是debug的配置

    {
        "version": "0.2.0",
        "configurations": [
    
            {
                "type": "lldb",
                "request": "launch",
                "name": "Debug",
                //"program": "${workspaceFolder}/test.out",
                //上一行是官方写法,但是不同的cpp调试都要改配置,非常麻烦
                "program": "${workspaceFolder}/${fileBasenameNoExtension}",
                "args": [],
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "Build with Clang"
            }
        ]
    }
    
  • 2 c_cpp_properties.json

    {
        "configurations": [
            {
                "name": "Mac",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [],
                "macFrameworkPath": [],
                "compilerPath": "/usr/local/Cellar/gcc/11.2.0/bin/g++-11",  // 使用brew list gcc查找到C++11的编译器路径
                "cStandard": "c11",  // 修改为c11标准 
                "cppStandard": "c++11",  // 修改为c++11标准 
                "intelliSenseMode": "macos-gcc-x64"
            }
        ],
        "version": 4
    }
    
NOTE:如果要配置opencv环境:
  • 查找opecv库的路径

    brew info opencv
    

    vscode配置C/C++笔记(硬要使用C++11)_第1张图片
    vscode配置C/C++笔记(硬要使用C++11)_第2张图片

  • 3 settings.json 解决“C++应输入声明”的错误

    {
        "C_Cpp.errorSquiggles": "Disabled"
    }
    
  • 4 tasks.json

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Build with Clang",//这个任务的名字在launch.json最后一项配置
                "type": "shell",
                "command": "clang++",
                "args": [
                    "-std=c++11",
                    "-stdlib=libc++",
                    //"test.cpp",这里是官方写法,不具有普遍性,注意两个配置文件的统一性即可
                    "${fileBasenameNoExtension}.cpp",
                    "-o",
                    //"test.out",
                    "${fileBasenameNoExtension}",
                    "--debug"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

配置完上述4个文件后,在debug界面点击debug可正确执行
vscode配置C/C++笔记(硬要使用C++11)_第3张图片
但直接运行文件时,依然不会按照c++11语法进行编译,解决办法如下

https://www.cnblogs.com/TAMING/p/10177322.html

1 安装Code Runner
2 依次找到:文件>首选项>设置>用户设置>扩展>run code configurate>Executor Map
3 然后点击在settings.json中编辑
vscode配置C/C++笔记(硬要使用C++11)_第4张图片
vscode配置C/C++笔记(硬要使用C++11)_第5张图片
4 如果发现无code-runner.executorMap配置,仅需在末尾输入code-runner.executorMap利用自动补全按一下回车,会生成各个语言的模板
vscode配置C/C++笔记(硬要使用C++11)_第6张图片
找到对应的语言这里以cpp为例:
cd $dir && g++ $fileName -o $fileNameWithoutExt && d i r dir dirfileNameWithoutExt
其中
$dir代表文件路径
$fileName代表文件名
$fileNameWithoutExt代表文件名对应的可执行文件文件名
翻译过来就是切换(cd指令)到源文件所在路径,编译(g++)源文件,输入(-o参数)成对应文件名的可执行文件,在执行
比如将c++默认的g++指令换成clang(clang编译指令格式为clang x.cpp -o x.exe所以其他不用修改,具体的指令需要根据编译器命令行参数来确定)

  • 5 run code configurate——setting.json
    根据我的需求,我只需要加个参数-std=c++11

    "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    
  • 完整文件:

    {
        "git.ignoreLegacyWarning": true,
        "window.zoomLevel": 2,
        "terminal.integrated.inheritEnv": false,
        "editor.suggestSelection": "first",
        "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
        "bracket-pair-colorizer-2.depreciation-notice": false,
        "files.associations": {
            "*.json": "cpp"
        },
        "files.autoSave": "afterDelay",
        "grunt.autoDetect": "on",
        "code-runner.saveFileBeforeRun": true,
        "code-runner.clearPreviousOutput": true,
        "code-runner.runInTerminal": true,
        "code-runner.executorMap": {
    
            "javascript": "node",
            "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
            "c": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
            "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $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"
        }
    }
    
  • 6 每次执行前都清空终端的配置方法
    安装Code Runner。mac下按shift + command + X,搜索Code Runner,点击设置图标,选择“扩展配置”,将Clear Previous Output的复选框勾上:
    vscode配置C/C++笔记(硬要使用C++11)_第7张图片

你可能感兴趣的:(系统安装,vscode,c++,c语言)