kkUbuntuLinux学习

Ubuntu快速搭建C++开发环境(VS Code编辑器)

2019-04-03  本文已影响484人  小古银

以下安装的是g++-8(目前最新)Visual Studio Code,此方法适用于Ubuntu 14.04 64位Ubuntu 16.04 32位/64位Ubuntu 18.04,Ubuntu 14.04 32位及以下系统无效。打开终端并且输入以下命令即可。

安装g++-8

  1. 安装software-properties-common:

    sudo apt-get -y install software-properties-common
    
  2. 添加PPA到库并更新(会提示按回车继续执行,此时按回车即可):

    sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    
  3. 更新软件信息:

    sudo apt-get update
    
  4. 安装g++:

    sudo apt-get -y install g++-8
    
  5. 将g++指向g++-8:

    sudo ln -sf /usr/bin/g++-8 /usr/bin/g++
    
  6. 显示g++版本号,如果正常显示版本号意味着安装成功:

    g++ --version
    

安装Visual Studio Code

  1. 安装libgconf库:

    sudo apt-get install -y libgconf-2-4
    
  2. 安装git:

    sudo apt-get -y install git
    
  3. 添加PPA到库并更新(会提示按回车继续执行,此时按回车即可):

    sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
    
  4. 更新软件信息:

    sudo apt-get update
    
  5. 安装ubuntu-make:

    sudo apt-get -y install ubuntu-make
    
  6. 通过ubuntu-make安装Visual Studio Code;过程中会询问安装路径,此时不需要修改直接按回车即可;然后会询问是否接受协议,此时输入a然后回车即可:

    sudo umake ide visual-studio-code
    
  7. 创建软链接到程序目录下:

    sudo ln -sf `env | grep ^HOME= | cut -c 6-`/.local/share/umake/ide/visual-studio-code/bin/code /usr/bin/code
    
  8. 创建项目目录:

    mkdir ~/Projects
    
  9. 显示Visual Studio Code版本号,如果正常显示版本号意味着安装成功:

    code --version
    
  10. 运行VS Code:

    code
    
  11. 安装中文语言包:

    1. 按下键盘Ctrl+Shift+X
    2. 在输入框里输入Chinese
    3. 按下中文(简体)后面的install
    4. 安装完后按下Ctrl+Shift+P打开命令面板
    5. 输入config后选择配置语言命令
    6. 选择Configure Display Language
    7. "locale":"en"改成"locale": "zh-cn",然后按下键盘Ctrl+S保存源文件
    8. 关闭VS Code
    安装中文语言包 步骤1
    安装中文语言包 步骤2
    安装中文语言包 步骤3

创建第一个C++项目(以下步骤除非有特殊说明,否则每次创建项目都要执行一次):

  1. 创建项目目录cppdemo,用于学习创建第一个C++项目,并进入cppdemo目录中:

    mkdir ~/Projects/cppdemo && cd ~/Projects/cppdemo
    
  2. 创建配置目录:

    mkdir ./.vscode
    
  3. 添加编译配置文件(输入以下内容然后按回车):

    cat > .vscode/tasks.json << EOF
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build",
                "type": "shell",
                "command": "g++",
                "args": [
                    "-std=c++17",
                    "-Wall",
                    "-Wextra",
                    "-g",
                    "-ggdb",
                    "mycpp.cpp",
                    "-o",
                    "demoapp.out"
                ],
                "group": "build",
                "presentation": {
                    "reveal": "always",
                },
                "problemMatcher": "\$gcc"
            }
        ]
    }
    EOF
    
  4. 添加运行配置文件(输入以下内容然后按回车):

    cat > .vscode/launch.json << EOF
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "\${workspaceFolder}/demoapp.out",
                "args": [],
                "stopAtEntry": false,
                "cwd": "\${workspaceFolder}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "build"
            }
        ]
    }
    EOF
    
  5. 添加智能提示配置文件(输入以下内容然后按回车):

    if [ `getconf LONG_BIT` -eq "64" ]; then
    cat > .vscode/c_cpp_properties.json << EOF
    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "/usr/include/c++/8",
                    "/usr/include/x86_64-linux-gnu/c++/8",
                    "/usr/include/c++/8/backward",
                    "/usr/lib/gcc/x86_64-linux-gnu/8/include",
                    "/usr/local/include",
                    "/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed",
                    "/usr/include/x86_64-linux-gnu",
                    "/usr/include",
                    "${workspaceRoot}"
                ],
                "defines": [],
                "intelliSenseMode": "clang-x64",
                "browse": {
                    "path": [
                        "/usr/include/c++/8",
                        "/usr/include/x86_64-linux-gnu/c++/8",
                        "/usr/include/c++/8/backward",
                        "/usr/lib/gcc/x86_64-linux-gnu/8/include",
                        "/usr/local/include",
                        "/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed",
                        "/usr/include/x86_64-linux-gnu",
                        "/usr/include",
                        "${workspaceRoot}"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                }
            }
        ],
        "version": 3
    }
    EOF
    else
    cat > .vscode/c_cpp_properties.json << EOF
    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "/usr/include/c++/8",
                    "/usr/include/i386-linux-gnu/c++/8",
                    "/usr/include/c++/8/backward",
                    "/usr/lib/gcc/i686-linux-gnu/8/include",
                    "/usr/local/include",
                    "/usr/lib/gcc/i686-linux-gnu/8/include-fixed",
                    "/usr/include/i386-linux-gnu",
                    "/usr/include",
                    "${workspaceRoot}"
                ],
                "defines": [],
                "intelliSenseMode": "clang-x64",
                "browse": {
                    "path": [
                        "/usr/include/c++/8",
                        "/usr/include/i386-linux-gnu/c++/8",
                        "/usr/include/c++/8/backward",
                        "/usr/lib/gcc/i686-linux-gnu/8/include",
                        "/usr/local/include",
                        "/usr/lib/gcc/i686-linux-gnu/8/include-fixed",
                        "/usr/include/i386-linux-gnu",
                        "/usr/include",
                        "${workspaceRoot}"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                }
            }
        ],
        "version": 3
    }
    EOF
    fi
    
  6. 创建C++源文件mycpp.cpp(代码就是写在这个文件):

    touch mycpp.cpp
    
  7. 用Visual Studio Code打开当前工作环境(不要忽略最后的点哟):

    code .
    
  8. 双击mycpp.cpp打开文件,第一次会提示安装C++插件,点击安装,然后等待右下角消息安装完成(只需执行一次,以后都不需要再执行)。

安装C++扩展
  1. 双击打开mycpp.cpp,然后输入以下代码:

    #include <iostream>
    
    int main(void)
    {
        std::cout << "小古银的C++教程" << std::endl;
        return 0;
    }
    
    第一个 Hello world 程序
  2. 按下Ctrl+Shift+B,然后选择build,就会开始编译。

    编译
  3. 编译完后,在终端窗口按回车键关闭终端。

    编译完按回车关闭终端
  4. 点击菜单栏中的终端,然后点击新建终端,就会打开终端窗口。

    新建终端窗口
  5. 然后在终端输入以下内容,就会运行程序:

    ./demoapp.out
    
  6. 运行程序将会显示小古银的C++教程

    运行程序 输出“小古银的C++教程”

    此时,第一个项目,也就是第一个程序就完成了。

上一篇下一篇

猜你喜欢

热点阅读