C++环境搭建

ubuntu 18.04+vscode 多文件C++编译环境搭建

​ 按教程完成搭建后,发现针对有多个cpp文件,依然存在无法编译的问题,多次尝试后找到两个解决办法:

使用code runner插件

在拓展插件商店内下载code runner,重启vscode后在code runner拓展里打开setting.json,找到executorMap设置项,将c程序对应的一行中的gcc $filename修改为gcc *.c,将c++对应一行中的g++ filename修改为g++ *.cpp,这样即可保证文件夹下所有程序文件均可被编译,具体配置如下图。code runner是一个支持多种语言的插件,对新手相当友好。
修改完成后

修改launch.json与task.json

第二种方法较为传统,修改默认生成的launch.json与task.json

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g", "*.cpp", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
        }
     ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值