Linux系统下VScode配置C/C++中的task.json和launch.json、setting.json对应的内容,实现多文件编译,包分离

本文详细介绍了如何在VSCode中配置C/C++项目的task.json(用于编译)和launch.json(用于调试),以及如何通过settings.json管理运行输出。作者分享了包分离、多文件编译和调试设置的方法,包括使用CodeRunner插件和调整配置参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vscode配置C/C++中的task.json、lauch.json些许困难,经过多次查看并研究,发现task.json和lauch.json对应的是Vscode终端中的内容。

右键RunCode对应的是setting中的设置,对应的是是输出

设置一下settings

这里我安装了CodeRunner的插件。然后使用快捷键ctrl+, 记住是逗号。输入Runner

点击setting.json

将&fileName改为*.cpp

成功运行了

包分离的话

我是将编译的包都放在build下面

修改这三个json文件,记住task是运行的,lauch是调试的,这俩对应的终端的。settings对应的是输出(Runcode运行)。

task中多文件的话需要修改

这里的运行文件对应的是lauch,

修改这里的-g后面的内容,变为*.cpp即可。

运行期间发现

这上边的俩有延迟性,可能是只负责运行生成成功的,先运行这个图之后就运行一一致了。

完整的json如下:

tasks.json 

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ 生成活动文件",
			"command": "/usr/bin/g++",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"*.cpp",//${file}
				"-o",
				"${fileDirname}/build/${fileBasenameNoExtension}"//${fileBasenameNoExtension}
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "编译器: /usr/bin/g++"
		}
	]
}

 lauch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}",//${workspaceFolder}/helloworld${fileBasenameNoExtension}
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

settings.json

{
    "workbench.colorTheme": "Default Dark Modern",
    "cmake.configureOnOpen": true,
    "C_Cpp.default.compilerPath": "/usr/bin/g++",
    "code-runner.executorMap": {

        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc *.cpp -o $workspaceRoot/build/$fileNameWithoutExt && $workspaceRoot/build/$fileNameWithoutExt",//cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
        "zig": "zig run",
        "cpp": "cd $dir && g++ *.cpp -o $workspaceRoot/build/$fileNameWithoutExt && $workspaceRoot/build/$fileNameWithoutExt",//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": "runghc",
        "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",
        "mojo": "mojo run"
    },
    "workbench.editor.editorActionsLocation": "titleBar",
    "window.customTitleBarVisibility": "auto"
}

 配置json参考一个还不错的视频,特此感谢

带你配置Linux系统下vscode的C语言/C++编译环境 (Ubuntu),保姆级实操跟做,可能是全B站最清晰简单的教程_哔哩哔哩_bilibili

 配置的官方文档

官方文档:https://code.visualstudio.com/docs/cpp/config-linux#_running-the-build

官方文档翻译版:https://blog.youkuaiyun.com/icacxygh001/article/details/120981354

### 创建 `settings.json` `launch.json` 文件 在 Visual Studio Code 中,为了更好地管理配置项目,可以手动创建并编辑 `settings.json` `launch.json` 配置文件。 #### settings.json 的创建与配置 对于 C/C++ 或 Python 项目的全局设置,可以在工作区根目录下找到 `.vscode` 文件夹。如果该文件夹不存在,则可自行创建此文件夹,并在其内部新建名为 `settings.json` 的 JSON 文件[^1]: ```json { "files.autoSave": "afterDelay", "editor.tabSize": 4, "C_Cpp.default.cppStandard": "c++17" } ``` 上述示例展示了部分常见的 VSCode 设置项,括自动保存延迟、缩进大小以及默认使用的 C++ 标准版本。 #### launch.json 的创建与配置 同样,在 `.vscode` 文件夹内建立一个新的 JSON 文件命名为 `launch.json` 来定义调试器的行为参数。以下是针对不同编程语言的具体实例[^2]: ##### 对于 Python: ```json { "version": "0.2.0", "configurations": [ { "name": "Python: 当前文件", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal" } ] } ``` ##### 对于 C/C++ (假设已安装 Microsoft 提供的扩展): ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe", // 编译后的程序路径 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "cmake build", // 构建任务名称 "miDebuggerPath": "/usr/bin/gdb", "logging": {"trace":true,"traceResponse":true,"engineLogging":true}, "internalConsoleOptions": "openOnSessionStart" } ] } ``` 通过以上方式,可以根据实际需求调整这些配置文件的内容来满足特定开发场景下的个性化要求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值