Linux下配置VSCode的C语言开发环境

文件目录结构

Linux下配置VSCode的C语言开发环境

包含目录

按ctrl + shift + P快捷键,选择“C/C++:编辑配置(JSON)”
Linux下配置VSCode的C语言开发环境
进行包含文件的相关配置如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

编译配置

菜单栏选择“终端”,再选择“配置默认生成任务”,则再工作区的.vscode文件夹下生成如下所示的名为“task.json”的文件。
在此文件中,“args”参数中的“-g”参数的值默认为"${file}",表示只编译当前文件(如,当活动文件为“main.c”时,按下ctrl+shift+B,则只编译“main.c”)。由于添加了头文件,该头文件对应的源文件也需要编译,该源文件与“main.c”位于同一目录,所以,“-g”参数的值改为"${workspaceFolder}/src/*.c",表示编译该目录下的所有源文件,当然也可以具体逐个指定源文件名称。

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: gcc build active file",
			"command": "gcc",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				// "${file}",	//只编译当前活动文件
				// "main.c", "calculate.c",	//编译指定的若干文件
				"${workspaceFolder}/src/*.c",	//编译文件夹下的所有源文件
				"-o",
				"${workspaceFolder}/out/${fileBasenameNoExtension}",
				"-I",
				"${workspaceFolder}/include"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /usr/bin/gcc"
		}
	]
}

调试配置

{
    "configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/out/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
    ]
}
上一篇:c/c++_Visual Studio Code配置


下一篇:2021-05-01