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++ 编译运行环境的三种方案       

<think>嗯,用户想在Ubuntu中用VSCode运行C程序,我得一步步帮他们配置。首先,可能需要安装VSCode本身。记得在Ubuntu里可以通过snap或者直接下载deb包来安装。然后,用户需要安装C编译器,比如gcc,这应该是通过sudo apt install build-essential命令来安装的。接下来,VSCode需要安装C/C++扩展,这扩展能提供语法高亮和调试支持。 然后,配置环境方面,用户可能需要创建工作区文件夹,用VSCode打开。然后配置tasks.json和launch.json文件,这样编译和调试才能顺利进行。tasks.json告诉VSCode如何编译代码,而launch.json配置调试器。可能还需要安装GDB作为调试工具,不过build-essential应该已经包含了。 用户可能会遇到问题,比如终端没有显示,这时候需要检查快捷键Ctrl+~来调出终端。另外,第一次创建C文件时,VSCode可能会提示安装C/C++的配置文件,用户需要允许这个操作。如果编译失败,可能是路径设置不对,需要检查tasks.json里的"command"是否是gcc,还有参数是否正确。 另外,用户可能想知道如何运行程序,除了调试,还可以直接在终端里用gcc编译然后运行。但集成在VSCode中更方便,所以配置好之后,应该可以一键运行。还要注意代码保存后才能正确编译,否则可能会有错误。 最后,用户可能需要一些验证步骤,比如写个Hello World程序,确保配置正确。如果还有问题,可能需要检查扩展是否安装正确,或者重新加载VSCode。总之,步骤需要详细但清晰,避免遗漏关键点,比如安装扩展或配置JSON文件,这些对新手来说可能不太熟悉。</think>### 在 Ubuntu 中配置 VSCode 运行 C 程序的步骤 #### 1. 安装必要工具 - **安装 VSCode** 通过以下命令或从[官网下载](https://code.visualstudio.com/)安装: ```bash sudo snap install code --classic ``` - **安装 C 语言编译器** 安装 `gcc` 和构建工具: ```bash sudo apt update && sudo apt install build-essential ``` #### 2. 配置 VSCode 环境 - **安装 C/C++ 扩展** 打开 VSCode,搜索并安装官方扩展 `C/C++`(由 Microsoft 提供),支持语法高亮和调试功能[^3][^4]。 - **创建工作目录** 在 Ubuntu 中新建文件夹(如 `c_projects`),右键选择“用 VSCode 打开”。 #### 3. 配置编译器路径 - **生成配置文件** 创建或打开任意 `.c` 文件后,按 `Ctrl+Shift+P` 搜索 `C/C++: Edit Configurations (JSON)`,自动生成 `c_cpp_properties.json`,确保编译器路径为 `gcc`。 - **配置编译任务** 按 `Ctrl+Shift+P` 搜索 `Tasks: Configure Task`,选择 `C/C++: gcc build active file`,生成 `tasks.json`。示例配置: ```json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C Compile", "command": "gcc", "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"] } ] } ``` #### 4. 编写并运行程序 - **示例代码** 创建 `hello.c`: ```c #include <stdio.h> int main() { printf("Hello World\n"); return 0; } ``` - **编译运行** 按 `F5` 启动调试(需配置 `launch.json`),或直接在终端执行: ```bash gcc hello.c -o hello && ./hello ``` #### 5. 调试配置(可选) - 按 `Ctrl+Shift+D` 创建 `launch.json`,设置调试器路径为 `gdb`[^3]: ```json { "configurations": [ { "name": "C Debug", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "cwd": "${workspaceFolder}", "externalConsole": false, "MIMode": "gdb" } ] } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值