在Windows下和Ubuntu系统下的配置VScode C++开发环境

最近在刷编程题,在Windows系统下,使用visual studio,每道题都新建一个工程太过繁琐,所有使用一个轻量化的系统。
本文主要介绍在Windows系统和Ubuntu系统下如何配置VScode,实现c++工程编译及调试的配置。

Ubuntu系统下vscode的c++配置过程

1.环境配置

在Ubuntu系统下,首先要安装必要的编译系统。gcc,g++,cmake等。

sudo apt-get update
sudo apt-get install build-essential 
sudo apt-get install cmake
sudo apt-get install cmake-gui

2.launch.json

新建一个cpp文件,写一个简单的hello world代码,会自动提示安装一个c/c++的插件。如果系统安装了gcc编译器(c++需要使用g++),可以直接F5运行,如果没有配置文件,
选择c++(GDB/LLDB)->选择期望使用的编译器。gcc编译c语言,g++编译c/c++语言。vscode会自动生成一个。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++-7 build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++-7 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

修改

"program": "${fileDirname}/${fileBasenameNoExtension}"

"program": "${fileDirname}/build/${fileBasenameNoExtension}"

这样生成的可执行文件会放到build子目录下。
增加一行

"preLaunchTask": "g++-7 build active file"

用来在调试前编译文件

修改完成后:

{
    // 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": "g++-7 build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++-7 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

3.task.json文件生成

Ctrl+Shift+P打开控制台,输入task,选择和launch文件中对应的preLaunchTask对应的任务。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++-7 build active file",
            "command": "/usr/bin/g++-7",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

修改args参数组下的

"${fileDirname}/${fileBasenameNoExtension}"

"${fileDirname}/build/${fileBasenameNoExtension}"

这个路径要和launch.json文件中对应。
修改完成后:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++-7 build active file",
            "command": "/usr/bin/g++-7",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/build/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

4.调试文件

在要调试的文件中设置断点,按F5,如果程序编译通过,则可以运行到断点处,进行调试。

5.总结:

这个方法只适用于简单的工程,对于复杂的工程,还是建议使用cmake来管理。

Windows系统下vscode的c++配置过程

在Windows系统下则比较复杂:

1.环境变量

INCLUED:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\winrt

LIB:

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\lib\x64

使用不同的msvc版本和winkit版本,路径的前缀略有不同,64位和32的LIB路径也不同。

2.新建c_cpp_properties.json

Ctrl+Shift+P打开控制台,输入 Edit Configurations,选择C/Cpp: Edit Configurations(json)选项。自动生成这个json文件。

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

可以直接使用默认生成的,也可以根据需要修改 “windowsSdkVersion”: “10.0.17763.0”,和环境变量中的INCLUDE下路径匹配即可。

3.新建launch.json

点击F5,选择c++(Windows)->default configuration。会自动生成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": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ]
}

修改

"program": "enter program name, for example ${workspaceFolder}/a.exe"

"program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe"

这样生成的中间文件和exe文件会放到当前目录下的build子目录下面。

"externalConsole": false

代表是使用vscode内置的命令行还是使用系统的cmd命令行。

增加一行:

"preLaunchTask": "msvc build" 

这个task会在后面新建,在launch前执行build操作。

4.新建tasks.json

Ctrl+Shift+P打开控制台,输入task,选择tasks:cofigure default build task->从模板构建->其它。自动生成

tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

其中,把

"label": "echo"

行改为:

"label": "msvc build"

 "command": "echo Hello"

行改为:

 "command": "cl.exe"

要确保cl.exe在环境变量PATH路径中,即可以在cmd中直接执行cl.exe而不报错。
在这里插入图片描述
其余改动:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msvc build",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/Zi",
                "/Fe:",
                "${fileDirname}/build/${fileBasenameNoExtension}.exe",
                "${file}",
            ],
            "group":  {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

其中args选项设置编译参数,把生成的可执行文件放到build子目录下,这儿的目录要和launch.json中

"program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe"

的目录匹配。
fileBasenameNoExtension 关键字代表当前选择的文件,不带后缀,这样可以在一个vscode工程中新建多个cpp文件,每个cpp中都可以有main函数,F5按键会执行编译并调试当前打开的cpp文件。

5.调试程序

在要执行的文件打断点,按F5,如果没有报错,则可执行到断点处,进行调试。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在Ubuntu 20.04上安装配置VSCode的C/C++开发环境,可以按照以下步骤进行: 1. 打开终端,运行以下命令安装VSCode: sudo snap install --classic code 2. 安装C/C++扩展,可以在VSCode中搜索并安装" C/ C++ "扩展。 3. 安装C和C++编译器。可以通过以下命令进行安装: sudo apt install build-essential 4. 配置编译器路径,打开VSCode,进入"文件" -> "首选项" -> "设置",搜索" C/ C++" ,并点击"C/ C++ Configuration "。在"C/ C++ Configuration "中,找到"C-Compiler Path"和"C++-Compiler Path"选项,将其设置为相应的编译器路径。一般情况下,C编译器路径为"/usr/bin/gcc",C++编译器路径为"/usr/bin/g++"。 5. 在VSCode中新建一个C/C++项目即可开始开发。 ### 回答2: Ubuntu 20.04 是一款非常好用的操作系统,对于开发者来说,这个系统有非常多的软件包可以使用。在这个系统中,如果需要进行 C/C++ 开发,那么 Visual Studio Code(简称 VSCode)就是一个非常好的选择。下面是在 Ubuntu 20.04 系统下安装和配置 VSCode 的 C/C++ 开发环境的步骤。 第一步:安装 VSCodeUbuntu 20.04 系统中,安装 VSCode 非常简单。只需要打开终端,输入以下命令即可: ``` sudo apt update sudo apt install code ``` 这个命令会自动下载并安装最新版本的 VSCode。 第二步:安装 C/C++ 扩展 安装 VSCode 后,需要通过扩展安装 C/C++ 开发环境。打开 VSCode,点击左侧的扩展图标(个人感觉 Ctrl+Shift+X 快捷键还是比较方便的),然后在搜索框中输入“C/C++”,选择第一个搜索结果,点击安装按钮即可。由于需要联网下载,可能需要等待一段时间。 第三步:安装 C/C++ 编译器 在 VSCode 中安装 C/C++ 扩展后,还需要在 Ubuntu 20.04 下安装 C/C++ 编译器。 如果希望使用 gcc 编译 C/C++ 程序,可以运行以下命令: ``` sudo apt update sudo apt install build-essential ``` 这个命令会安装编译器及其相关的库文件。 如果希望使用 llvm/clang 编译 C/C++ 程序,可以运行以下命令: ``` sudo apt update sudo apt install clang ``` 这个命令会安装 llvm/clang 编译器及其相关的库文件。 第四步:配置任务 在 VSCode 中打开一个 C/C++ 项目后,可以通过菜单栏中的“终端”菜单打开集成终端(Ctrl+Shift+`)。进入终端后,可以通过以下命令编译 C/C++ 程序: ``` gcc -o hello_world hello_world.c ``` 如果希望简化编译过程,可以在 VSCode 的“任务”中配置编译任务。具体步骤为:打开 VSCode 的“任务”面板(默认快捷键 Ctrl+Shift+B),然后点击“配置任务”按钮,选择“终端”任务,然后在任务配置文件(tasks.json)中添加以下内容: ``` { "version": "2.0.0", "tasks": [ { "label": "Build", "type": "shell", "command": "gcc", "args": [ "-o", "${fileBasenameNoExtension}.out", "${file}" ], "group": { "kind": "build", "isDefault": true } } ] } ``` 上面这个配置文件会在文件夹中创建“build”文件夹,并把编译器输出的可执行文件放到这个文件夹中。 第五步:开始编程 以上步骤完成后,就可以在 VSCode 中愉快地开发 C/C++ 程序了! ### 回答3: Ubuntu 20.04是一款非常受欢迎的操作系统,而VSCode是一款非常流行的代码编辑器,其可提供C/C++开发的全面支持。下面是安装和配置VSCode进行C/C++开发的步骤: 第一步,从下面的命令中进行安装: sudo apt update sudo apt install software-properties-common apt-transport-https wget wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add - sudo add-apt-repository “deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main” sudo apt update sudo apt install code 第二步,打开VSCode并单击设置,然后打开“Extensions”选项。在搜索栏中输入"C/C++",选择"ms-vscode.cpptools"进行安装。 第三步,创建您的第一个C/C++项目。在终端中运行以下命令: mkdir my_cpp_project cd my_cpp_project code . 第四步,在类型选择器中选择“C++”,然后填写以下信息: 名称:My First C++ Project 位置:/home/USERNAME/my_cpp_project 第五步,单击“Create”以创建新项目。 第六步,将以下行添加到您的.vscode/tasks.json文件中: { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true } } ] } 第七步,保存文件并按F5运行调试器。 此时,您应该已经成功配置了C/C++开发环境,可以成功编写和调试C/C++代码了。总之,这是一个易于操作的过程,需要一些时间和耐心来完成,但一旦安装完成,您就可以开始创建和运行C/C++项目了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仟人斩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值