ubuntu18.04- +vscode c++使用时的三个配置文件

 用vscode,如果只是编译单个文件,无需任何配置文件,用插件 C/C++ Compile Run,F6 ,F7即可出结果。
 windows:装MinGW;liunx :装GCC,G++ ;MAC :装CLANG。

vscodec++编译时 三个配置文件,c_cpp_properties.json:引用库文件配置(ctrl + shift + P)
                                                        task.json:                   编译器配置
                                                       launch.json:               任务配置
以下3个是本人ubuntu18.04版本上运行的,亲测可行。供大家参考。
三个配置文件每个的重点项标序号的,其余为一般性解释。

1.单文件配置:如果是单文件编配置以下两个文件即可。
launch.json: 

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gdb Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/myPrjmain.o",// 1.debug时运行的是哪个程序 主要是后缀不同   win10 :.exe,     ubuntu:.o,    unix :.out //${fileDirname}/${fileBasenameNoExtension}.exe                                       
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",                               //2. debug工具        win10 : MinGW  , ubuntu :gdb, MAC : lldb
         
            "setupCommands": [
                {
                      "description": "为 gdb 启用整齐打印",
                      "text": "-enable-pretty-printing",
                                        "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++",
            "miDebuggerPath": "/usr/bin/gdb"       //3.编译运行 cpp文件时才能找到 gdb程序
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",        // 1.任务名称,与launch.json的preLaunchTask相对应
            "type": "shell", 
            "command": "g++",//2.要使用的编译器
            "args": 
            ["-g", 
               "${workspaceFolder}/src/myPrjmain.cpp", 
                "-o", 
                "${workspaceFolder}/myPrjmain.o", //3.21必须与 launch.json 中的 ${workspaceFolder}/${fileBasenameNoExtension}.o对应
                ],  
                "group": {
                    "kind": "build",
                    "isDefault": true // 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件,我这里不多提
                },
                "presentation": {
                    "echo": true,
                    "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never。具体参见VSC的文档
                    "focus": false,           // 设为true后可以使执行task时焦点聚集在终端,但对编译c和c++来说,设为true没有意义
                    "panel": "shared"   // 不同的文件的编译信息共享一个终端面板
                }
        }
    ]
}

2.多文件配置。如要要添加一些库头文件或者自定义文件,要添加c_cpp_properties.json 修改task.json
     注意:与前面两个文件不同 ,c_cpp_properties.json是不可以有注释的。
以下是c_cpp_properties.json
 

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/home/usr/C++/myprj/inlcude"      
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

tasks.json
 

 "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",       
            "type": "shell", 
            "command": "g++",
            "args": 
            ["-g", 
                "-I", "/home/usr/C++/myprj/inlcude",                      //1.添加自定义头文件路径
                "/home/usr/C++/myprj/src/tree/treemain.cpp",              //2.添加自定义头文件包含的cpp文件
                "${workspaceFolder}/src/myPrjmain.cpp", 
                "-o", 
                "${workspaceFolder}/myPrjmain.o", 
                ],  
                "group": {
                    "kind": "build",
                    "isDefault": true 
                },
                "presentation": {
                    "echo": true,
                    "reveal": "always", 
                    "focus": false,           
                    "panel": "shared"  
                }
        }
    ]
}



 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值