• Linux环境下配置vscode的C/C++编译环境


    操作系统环境:  Linux

    配置vscode的C/C++编译环境需要安装插件:

    本文的配置是指在linux下不使用vscode插件中自动配置,而是采用手动编写配置文件。主要原因是插件自动生成的C/C++配置文件功能不全面,为了更好的适应C/C++的语言特性、编写功能更强大的C/C++语言,所以采用手动编写配置文件。

    ========================================================

    VSCODE中C/C++配置需要最少两个文件:   

      .vscode/task.json     

      .vscode/launch.json

    本文中demo的C语言代码:

    mainX.c

    #include<stdio.h>
    
    void main()
    {
        int a=0;
        a++;
        a+=2;
        a-=3;
        printf("a=%d\n",a);
        return;
    }

    运行结果:

    ===========================================================

      .vscode/task.json     为C/C++项目配置编译条件:

    {
        "tasks": [
            {
                "type": "shell",
                "label": "C/C++: gcc-7 生成活动文件",
                "command": "/usr/bin/gcc-7",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}111"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "调试器生成的任务。"
            }
        ],
        "version": "2.0.0"
    }

    "command": "/usr/bin/gcc-7",     指定c/c++编译器路径

    "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}111"
                ],

     “arg”  参数中“-g” 表示编译生成的可执行文件带有调试信息,我们一般习惯在该参数后指定需要编译的源文件,其中${file}指的是当前打开的当前文件,这里我们也可以改写该文件名,不然的话每次编译都要保证当前打开的文件是需要编译的文件,这里只的需要编译的文件是指 main 函数所在的文件。

    “-o” 是指编译后的文件存储地址和文件名,${fileDirname}指的是当前打开文件所在的目录, ${fileBasenameNoExtension}指的是当前打开文件的不带扩展名后的文件名,这里我们为了区别名称使用  ${fileBasenameNoExtension}111  意味着编译后的文件名为  mainX111  。

    "cwd": "${fileDirname}"  ,  “cwd” 指定当前目录
     
     
     
     
     
     
     
    ===================================================== 

      .vscode/launch.json     为C/C++项目配置运行条件:

    {
        // 使用 IntelliSense 了解相关属性。 
        // 悬停以查看现有属性的描述。
        // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "gcc-7 - 生成和调试活动文件",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}111",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "C/C++: gcc-7 生成活动文件",
                "miDebuggerPath": "/usr/bin/gdb"
            }
        ]
    }

    "program": "${fileDirname}/${fileBasenameNoExtension}111",             "program"指定需要执行的文件路径

    "preLaunchTask": "C/C++: gcc-7 生成活动文件",                                   "preLaunchTask" 指定运行编译好文件前需要执行的任务

    需要注意的是    "preLaunchTask"  中的值   "C/C++: gcc-7 生成活动文件"  需要和 task.json 中的"label" 值 "C/C++: gcc-7 生成活动文件"保持一致,否则的话运行编译后的文件前会找不到需要执行编译的配置信息。

    ========================================================

     .vscode/task.json 

    {
        "tasks": [
            {
                "type": "shell",
                "label": "build task",
                "command": "/usr/bin/gcc-7",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "调试器生成的任务。"
            }
        ],
        "version": "2.0.0"
    }

     .vscode/launch.json

    {
        // 使用 IntelliSense 了解相关属性。 
        // 悬停以查看现有属性的描述。
        // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "gcc-7 - 生成和调试活动文件",
                "type": "cppdbg",
                "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "build task",
                "miDebuggerPath": "/usr/bin/gdb"
            }
        ]
    }

     =============================================================

    参考资料:

    https://code.visualstudio.com/docs/editor/variables-reference

    Predefined variables

    The following predefined variables are supported:

    • ${workspaceFolder} - the path of the folder opened in VS Code
    • ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
    • ${file} - the current opened file
    • ${fileWorkspaceFolder} - the current opened file's workspace folder
    • ${relativeFile} - the current opened file relative to workspaceFolder
    • ${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder
    • ${fileBasename} - the current opened file's basename
    • ${fileBasenameNoExtension} - the current opened file's basename with no file extension
    • ${fileDirname} - the current opened file's dirname
    • ${fileExtname} - the current opened file's extension
    • ${cwd} - the task runner's current working directory on startup
    • ${lineNumber} - the current selected line number in the active file
    • ${selectedText} - the current selected text in the active file
    • ${execPath} - the path to the running VS Code executable
    • ${defaultBuildTask} - the name of the default build task
    • ${pathSeparator} - the character used by the operating system to separate components in file paths

     

    本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注者,如有侵权请与博主联系。
  • 相关阅读:
    安全传输平台项目扩展——keymngserver重构-硬件扩展
    安全传输平台项目扩展——C复习-C++复习-keymngclient重构
    安全传输平台项目——客户端代码移植-项目模块总结
    安全传输平台项目——配置管理终端-读写数据库
    根号分治刷题记录
    使用netsh命令来管理IP安全策略
    关于make_shared无法访问非公有构造函数的解决方法
    两两交换链表中的节点-递归解法
    Spring 的 AOP 简介
    Spring IOC和DI 注解开发
  • 原文地址:https://www.cnblogs.com/devilmaycry812839668/p/15630724.html
Copyright © 2020-2023  润新知