Ubuntu上vscode编译C++

VScode编译C++主要有如下几种种方法。

方法一、配置  launch.json 和 tasks.json 文件。

在安装完vscode,下载相应的插件后,编写C++程序并进行编译。在编译之前首先需要配置vscode的两个文件,

分别为: launch.json 和 tasks.json 。

1、下载编辑C++ 的相应插件: C/C++,如下图第一个插件

   

建立文件夹 test ,并在vscode中选择打开此文件夹 (vscode必须打开 .cpp文件所在的文件夹才能进编译,而不能只是打开一个 .cpp的单文件进行编译。)。在vscode中点击1处建立C01文件夹;在C01目录下,再点击2处,建立 .cpp 文件。目录结构如下图:

       

2、配置  launch.json 文件

 (1)、打开01.cpp文件,按F5,在出现的命令提示栏上选择 C++(GDB/LLDB),

 (2)、 然后在出现的launch.json模板上进行修改,主要修改2个地方。分别为下图的3处,为运行编译程序的路径;4处为添加的在启动运行程序之前,要进行先编译,与下一个文件 tasks.json 相对应。launch.json文件用于运行编译的程序。

附   launch.json文件配置结果如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${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
                }
            ]
        }
    ]
}

3、配置 tasks.json 文件:

   (1)、按 ctrl+shift+P 或者 F1,来启动命令提示栏。在其中输入 run task,选择 Run Task,

    

 (2)、再点选 No task to run found,Configure Tasks... 

    

 (3)、再点选 Create tasks.json file from template

    

      (4)、点选 Others

      

       (5)、对出现的 tasks.json 进行修改。label 与上面的launch.json 中的 preLaunchTask 相对应。command 相当于在bash中编译 .cpp文件时的命令。

    

  (6)、最终修改的结果如下:

    

附 tasks.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",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        //"panel": "shared",
        //"showReuseMessage": true,
        //"clear": false
    }
}

4、回到 01.cpp 文件,按F5即可编译运行该程序,结果如下所示:

方法二、安装插件 Code Runner 来进行编译,如下图第一个插件。

  

点击箭头出的小三角即可编译

方法三、安装 C/C++ Compile Run 插件,按F6 可直接进行编译运行 .cpp 文件

注意:linux 系统上必须要安装有gcc/g++;windows系统上必须要安装有mingw 才可以。

二、总结

1、方法一可以进行断点调试,方法二、三只能进行编译运行。

2、vscode的Variables Reference

Predefined variables :
${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
${relativeFile} - the current opened file 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


Predefined variables examples
Supposing that you have the following requirements:

A file located at /home/your-username/your-project/folder/file.ext opened in your editor;
The directory /home/your-username/your-project opened as your root workspace.
So you will have the following values for each variable:

${workspaceFolder} - /home/your-username/your-project
${workspaceFolderBasename} - your-project
${file} - /home/your-username/your-project/folder/file.ext
${relativeFile} - folder/file.ext
${fileBasename} - file.ext
${fileBasenameNoExtension} - file
${fileDirname} - /home/your-username/your-project/folder
${fileExtname} - .ext
${lineNumber} - line number of the cursor
${selectedText} - text selected in your code editor
${execPath} - location of Code.exe

 

参考资料:

[1]  构建Visual Studio Code编译调试Linux C++环境

[2]  在Ubuntu中用Visual Studio Code编译调试C\C++

[3]  Visual Studio Code 如何编写运行 C、C++ 程序

[4]  VS Code 搭建 C/C++ 编译运行环境的三种方案       

### 回答1: 1. 安装gcc编译器 在终端中输入以下命令: ``` sudo apt-get update sudo apt-get install build-essential ``` 2. 安装VS Code 在官网下载VS Code的.deb安装包,然后在终端中输入以下命令进行安装: ``` sudo dpkg -i <下载的.deb文件名> sudo apt-get install -f ``` 3. 安装C/C++扩展 在VS Code中打开扩展面板(快捷键:Ctrl+Shift+X),搜索并安装C/C++扩展。 4. 配置任务 在VS Code中打开终端(快捷键:Ctrl+Shift+`),输入以下命令: ``` mkdir .vscode cd .vscode touch tasks.json ``` 然后在tasks.json文件中输入以下内容: ``` { "version": "2..", "tasks": [ { "label": "build", "type": "shell", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true } } ] } ``` 5. 配置调试 在VS Code中打开调试面板(快捷键:Ctrl+Shift+D),点击“创建配置文件”,选择“C++(GDB/LLDB)”,然后在生成的launch.json文件中输入以下内容: ``` { "version": ".2.", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] } ``` 6. 编写代码 在VS Code中新建一个.c文件,输入以下代码: ``` #include <stdio.h> int main() { printf("Hello, world!\n"); return ; } ``` 7. 运行程序 在VS Code中按下F5键,程序将会编译并运行,输出“Hello, world!”。 ### 回答2: Ubuntu是一款非常常用的操作系统,与之配合使用的编辑器之一是VSCodeVSCode是一个强大的编辑器,拥有很多插件和扩展,可以在Ubuntu上配置C语言环境以提高编程效率。下面是详细的配置步骤: 1. 安装gcc编译器和调试工具(gdb) 使用以下命令进行安装: sudo apt-get update sudo apt-get install build-essential gdb 2. 安装VSCodeUbuntu的软件中心中下载并安装VSCode,或使用以下命令进行安装: sudo snap install --classic code 3. 安装C/C++ for Visual Studio Code扩展 在VSCode中搜索并安装C/C++ for Visual Studio Code扩展。此插件将VSCodeC++和C语言环境结合在一起,提供许多有用的功能,如代码自动完成功能、调试功能等。 4. 配置编译器路径 打开VSCode并选择“文件”>“首选项”>“设置”。在搜索框中输入“C_CPP: Default”并选择“C_CPP: Default:IntelliSense模式”下的“编辑”。在弹出的编辑窗口中,找到“配置”下的“Vcpath”选项。在该选项下输入编译器的路径(gcc的路径是/usr/bin/gcc),并保存更改。 5. 创建C语言文件 在VSCode中打开一个新文件夹并创建一个名为“main.c”的文件。在该文件中输入以下代码: #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6. 进行编译和运行 按下F5或选择“调试”>“开始调试”进行编译并运行您的代码。您应该可以看到输出“Hello World!”的结果。 以上就是在Ubuntu上配置VSCode C语言环境的详细步骤。通过这种方式,您可以使用UbuntuVSCode方便地进行C语言编程。 ### 回答3: Ubuntu作为一种流行的开源操作系统,广受IT从业者的喜爱。而Visual Studio Code作为一种比较优秀的代码编辑器,能够轻松满足很多开发者的需求。在Ubuntu上使用VSCode编辑C语言代码,我们需要先进行一些配置。 首先,我们需要安装C语言编译器。Ubuntu上默认情况下是没有C语言编译器的,我们需要手动进行安装。打开终端,输入以下命令: ``` sudo apt-get update sudo apt-get install build-essential ``` 这样就会将C语言编译器安装到Ubuntu上。 接下来,我们需要在VSCode中安装一些插件,来帮助我们更好地开发C语言代码。打开VSCode,在扩展商店中搜索以下插件进行安装: - C/C++:提供C/C++语言支持和代码补全功能。 - Code Runner:允许在VS Code中直接运行代码。 - C/C++ Clang Command Adapter:提供Clang格式的代码补全功能。 安装完毕后,我们需要在VSCode中打开C语言项目,配置运行环境。在打开的项目中,打开“launch.json”文件。该文件主要用于配置项目的调试器。我们需要选择“C++ (GDB/LLDB)”作为调试器。 ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb" } ] } ``` 在上述代码中,我们修改了以下内容: program:以“${workspaceFolder}/a.out”的形式指定C语言可执行文件位置。 miDebuggerPath:使用GDB作为调试器。 修改完成后,我们就可以在VSCode中编写C语言代码,并且使用Ctrl + Alt + N快捷键进行运行了。 在整个配置过程中,需要注意的是,一定要安装好必要的插件和配置文件。只有这些文件齐全,我们才能方便地进行C语言开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值