关于 Visual Studio Code 使用的正确姿势

学术版

览遍千秋 @ 2022-12-22 22:11:45

我已经通过 Visual Studio Code 配置了 C/C++,Python,现在由于工程学习的需要,准备再配置 Java 环境。

我根据 https://blog.csdn.net/rothschild666/article/details/125785468 完成了 VSC 的插件下载、Java JDK/JRE 的安装,但是在按照教程所示,以非调试模式执行时,调用了 C/C++ 的任务。

在 .vscode 文件夹下的文件和内容如下:

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

launch.json

{
    "configurations": [
        {
            "name": "C/C++: g++.exe 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Windows\\System32\\cmd",
            "args": ["/C", "${fileDirname}\\${fileBasenameNoExtension}.exe", "&", "pause"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ],
    "version": "2.0.0"
}
{
    "C_Cpp.errorSquiggles": "Disabled",
    "latex-workshop.latex.tools": [
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-pdf",
                "%DOCFILE%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "xelatex",
            "tools": [
                "xelatex"
            ],
        },
        {
            "name": "pdflatex",
            "tools": [
                "pdflatex"
            ]
        },
        {
            "name": "xe->bib->xe->xe",
            "tools": [
                "xelatex",
                "bibtex",
                "xelatex",
                "xelatex"
            ]
        },
        {
            "name": "pdf->bib->pdf->pdf",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        }
    ],
    "files.associations": {
        "iomanip": "cpp",
        "deque": "cpp",
        "string": "cpp",
        "vector": "cpp"
    },
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\Program Files\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-Wall",
                "-std=c++14",
                "-Wl,--stack=1024000000"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

事实上,对于 VSC 的配置我一直不是很明白,C/C++ 也是按照 LeavingZ 提供的教程完成的。在配置 Python 环境时也遇到过同样的问题,只是我把执行 Python 的快捷键从 Ctrl+Alt+N 修改为了 Ctrl+Alt+Q。

希望能得到关于 VSC 使用和配置的正确姿势。


by 览遍千秋 @ 2022-12-22 22:12:37

真心求解 请勿qp


by Eleveslaine @ 2022-12-22 22:36:34

@览遍千秋

建议您装一个 Code Runner 扩展,可以自动识别语言并生成相应的运行命令

我装完这个以后是可以成功生成编译命令的 虽然我没有Java环境


by sunrise1024 @ 2022-12-22 22:36:49

@览遍千秋 如果可以的话尝试将不同语言的源文件放在不同的文件夹中?


by 览遍千秋 @ 2022-12-22 22:37:44

@Franz_Liszt 已经装了 Code Runner 了


by Eleveslaine @ 2022-12-22 22:43:33

@览遍千秋 ???那就很雾,我家 Code Runner 连 php,perl 等一堆语言都能给我跑起来(当然只是生成编译命令


by Eleveslaine @ 2022-12-22 22:53:35

@览遍千秋 您看一下您右上角的运行箭头旁边的下拉菜单里有没有 Run Code 选项?那个是 Code Runner 运行入口


by Sweet_Wrapping_Grass @ 2022-12-22 23:01:48

@Franz_Liszt 奇了怪了我以前配好的可以运行 Java 代码的 VSCode 现在也跑不了了


by 览遍千秋 @ 2022-12-22 23:07:49

@Franz_Liszt 有的


by register_new @ 2022-12-22 23:22:44

览遍千秋 的楼下


by Eleveslaine @ 2022-12-23 07:46:02

@览遍千秋 我盲猜一波您用的是 VSCode 自带的运行功能。
Run Code 相当于 Code Runner 对运行按钮进行了重载,这样根本就不用配置什么 launch.json 了,直接用 Code Runner 的内置文件就可以运行任何语言的程序(如果有环境的话)。

它的自带 settings.json 存储了大量编译命令,以下是 code-runner.executorMap 中的内容:

"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 && $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",
        "sml": "cd $dir && sml $fileName"
    }

另外,我似乎没有理解,有 Run Code 也没有点它试一试吗?或者说,点击 Run Code 仍然出现语言错误的问题?


| 下一页