Ubuntu20.04 -- 小白系列1 之 vs code c++环境配置

发布于:2022-10-29 ⋅ 阅读:(371) ⋅ 点赞:(0)

1、新建要存储代码的文件夹(例如code)

        然后用终端打开输入code .

        自动弹出vscode

 2、然后在工作区右键新建文件 main.cpp,复制以下代码。

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

3、安装C++,C/C++ Extension Pack等插件(后面两个是数学工具,看你自己哈哈哈)

 

4、改写launch.json文件。

        点开复制下面代码改写,保存退出。(值得注意的是,"program": "${workspaceFolder}/build/code"的 /build/code是你的可执行文件的名字,后续cmake的时候会说清楚这个)

{
    // 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}/build/code",//你的cmake编译后的输出可执行文件的位置
            "args": ["input/SampleInput", "output/SampleOutput"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        }
    ]
}

5、改写task.json文件。

        ctrl + shift + p -> 输入task ->配置默认生成任务 ->cpp活动生成文件

        就会出现task.json文件。全部改写为以下代码,保存退出

{
    "version":"2.0.0",
    "options": {
        "cwd": "${workspaceRoot}/build" //执行命令的目录
    },
    "tasks":[
        {
            "label": "cmake",
            "type": "shell",
            "command": "cmake",
            "args": [
                ".."
            ],
        },
        {
            "label": "make",//要执行的第二个make命令
            "type": "shell",
            "command": "make",
            "args": [],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn":["cmake"],//指定,在执行make之前,需先执行cmake
        },
    ]
}

6、工作区创建 CMakeLists.txt文件。

        复制下面代码并保存


cmake_minimum_required(VERSION 3.0)

project(CODE)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

set(CMAKE_BUILD_TYPE Debug)

# include_directories(${CMAKE_SOURCE_DIR} include)  # cmakelist 所在目录的include

# include_directories(${CMAKE_SOURCE_DIR} src)

# link_libraries(pthread)

add_executable(code main.cpp)

7、终端安装camke

sudo apt install cmake

8、每次新建工作区或者新编译的时候,就要处理一次。

CMakeLists.txt 最后一行改写需要编译的文件和生成的可执行文件名(code),然后再去launch.json改写"program": "${workspaceFolder}/build/code"

这里最后的code就是生成的可执行文件名(code)。

打开刚才的main.cpp,然后ctrl + shift + ~ 打开终端,一条条输

mkdir build

cd build

cmake ..

make

这是再输入

ls

就会出现如下

 绿色的那个就是我们的可执行文件

输入

        ./code

即可运行。但是这样确实会比较麻烦,可以ctrl+F5,直接运行,F5调试。但由于vscode保留了以前c++的运行方式,所以其实很多教程都是从cmake,make开始

无论哪一种,都建议你学cmake语法,因为它只是一个文件关联的文件语言,而且每次用,其实可以几个文件全部复制,重命名即可。

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到