vscode ubuntu c++运行环境配置

9 篇文章 0 订阅
9 篇文章 0 订阅

        官方教程地址:Get Started with C++ on Linux in Visual Studio CodeGet Started with C++ on Linux in Visual Studio Code

1、下载安装vscode

Visual Studio Code - Code Editing. RedefinedVisual Studio Code - Code Editing. Redefined)。

2、安装C++扩展

3、安装gdb

apt-get install gdb

如果不安装gdb运行可能会报错“unable to start debugging. the value of miDebuggerPath is invalid.md”。

4、示例程序

        helloworld示例程序如下,保存为test.cpp。

#include <iostream>
using namespace std;
 
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

5、运行程序

5.1 task模式

        按下F5或点击左侧Run&Debug,点击create a launch.json File,弹窗中选择第一个即可运行程序,输出结果同时,工程文件夹下多出一个.vscode文件夹,包含launch.json, tasks.json, c_cpp_properties.json。

        (1)launch.json

        launch.json文件主要用来设置如何调试。
        直接点击左侧边栏的Run and Debug图标(Ctrl+Shirft+D),点击create a launch.json file,选择C++(GDB/LLDB),选择Default Configuration,就能自动生成launch.json文件。
        一般地只需要改"program":部分内容改为项目路径下生成的执行文件即可。
        如果需要调试前重新编译一遍,可以新增一条"preLaunchTask",里面的内容改为tasks.json中的'label'名称。

{
    // 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": "${workspaceFolder}/bin/hello_world", //只改这里
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "task1" //可以新增一条
        }
    ]
}

        (2)tasks.json

        tasks.json文件相当于vscode的.sh或.bat文件,用来记录一系列操作的宏,可以用来设置如何编译文件,如何运行文件,几乎.sh能干的都可以干。

        下面增加了一个build.sh脚本来删除和创建build文件夹,build.sh内容如下所示:

#!/bin/bash
if [ ! -d "bin" ]; then
    mkdir bin
fi
g++ main.cc -g -o ./bin/hello_world  #默认为release模式,-g表示debud模式,如果编译c程序,g++改成gcc

        按ctrl + shift + p打开vscode控制台,输入Tasks: Configure Task,再选择Create tasks.json file from templates,选择Others模板,就自动生成了一个tasks.json文件,这样你就可以在该文件中编写参数来调整设置。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "task1", //你的设置文件名,可随便起
            "type": "shell", //运行task的平台,一般是shell
            "command": "bash ./build.sh", //普通的shell命令
            "args": [],
            "group": {
                "kind": "build", //设置为build组,这样当你在vscode中执行build命令时,
                                 //就能自动执行"command"中的命令了
                "isDefault": true
            }
        }
    ]
}

        (3)c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

        整个过程描述:
        按F5 => 运行launch.json => 由于设置"preLaunchTask": "task1",先去运行tasks.json中的task1中的command => 运行build.sh => 调用g++将main.cc编译成可执行文件hello_world => 开始调试launch.json中program所指的可执行文件hello_world

5.2 cmake模式

        (1)cmake安装


(1)安装编译所需的依赖包:
sudo apt update
sudo apt install build-essential libssl-dev
(2)下载CMake的源代码。访问[CMake官网](https://cmake.org/files/)下载指定版本的源码压缩包。
wget https://cmake.org/files/v3.20/cmake-3.20.0.tar.gz
(3)解压源代码包,并进入解压后的目录
tar -zxvf cmake-3.20.0.tar.gz
cd cmake-3.20.0
(4)编译和安装:
./configure
make
sudo make install
(5)验证安装
cmake --version

        (2)build.sh和CMakeLists.txt

        工程目录下新建build.sh和CMakeLists.txt。
        build.sh内容如下。

cmake_minimum_required(VERSION 3.20)
project(hello_world)
 
set(CMAKE_BUILD_TYPE "Debug") # 默认是Release模式,设置为Debug才能调试
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) # 设置可执行文件的输出目录
 
add_executable(hello_world test.cpp)

        CMakeLists.txt内容如下。

#!/bin/bash
if [ ! -d "build" ]; then
    mkdir build
else
    rm -rf build/*
fi
cd build
Local_Dir=$(cd "$(dirname "$0")"; pwd)
echo "Now work at Dir:$Local_Dir"
cmake ..
make

        整个过程描述:
        按F5 => 运行launch.json => 由于设置"preLaunchTask": "task1",先去运行tasks.json中的task1中的command => 运行build.sh => 调用cmake并根据CMakeLists.txt文件编译出可执行文件hello_world => 开始调试launch.json中program所指的可执行文件hello_world。

5.3 CMake Tools

        安装插件CMake Tools。

        安装完成之后,直接点击下方build和运行按钮即可。安装完成之后可能需要重启vscode。

6、参考资料

[1] https://blog.csdn.net/weixin_43180456/article/details/129847004

[2] https://www.zhihu.com/question/30315894

[3] https://blog.csdn.net/A_L_A_N/article/details/104548022

【版权声明】
本文为博主原创文章,未经博主允许严禁转载,我们会定期进行侵权检索。  

更多python与C++技巧、三维算法、深度学习算法总结、大模型请关注我的博客,欢迎讨论与交流:https://blog.csdn.net/suiyingy,或”乐乐感知学堂“公众号。Python三维领域专业书籍推荐:《人工智能点云处理及深度学习算法》。

  • 27
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Coding的叶子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值