以下安装的是g++-8(目前最新)和Visual Studio Code,此方法适用于Ubuntu 14.04 64位、Ubuntu 16.04 32位/64位、Ubuntu 18.04,Ubuntu 14.04 32位及以下系统无效。打开终端并且输入以下命令即可。
安装g++-8
-
安装software-properties-common:
sudo apt-get -y install software-properties-common
-
添加PPA到库并更新(会提示按回车继续执行,此时按回车即可):
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
-
更新软件信息:
sudo apt-get update
-
安装g++:
sudo apt-get -y install g++-8
-
将g++指向g++-8:
sudo ln -sf /usr/bin/g++-8 /usr/bin/g++
-
显示g++版本号,如果正常显示版本号意味着安装成功:
g++ --version
安装Visual Studio Code
-
安装libgconf库:
sudo apt-get install -y libgconf-2-4
-
安装git:
sudo apt-get -y install git
-
添加PPA到库并更新(会提示按回车继续执行,此时按回车即可):
sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
-
更新软件信息:
sudo apt-get update
-
安装ubuntu-make:
sudo apt-get -y install ubuntu-make
-
通过ubuntu-make安装Visual Studio Code;过程中会询问安装路径,此时不需要修改直接按回车即可;然后会询问是否接受协议,此时输入
a
然后回车即可:sudo umake ide visual-studio-code
-
创建软链接到程序目录下:
sudo ln -sf `env | grep ^HOME= | cut -c 6-`/.local/share/umake/ide/visual-studio-code/bin/code /usr/bin/code
-
创建项目目录:
mkdir ~/Projects
-
显示Visual Studio Code版本号,如果正常显示版本号意味着安装成功:
code --version
-
运行VS Code:
code
-
安装中文语言包:
- 按下键盘
Ctrl+Shift+X
- 在输入框里输入
Chinese
- 按下中文(简体)后面的
install
- 安装完后按下
Ctrl+Shift+P
打开命令面板 - 输入
config
后选择配置语言命令 - 选择Configure Display Language
- 将
"locale":"en"
改成"locale": "zh-cn"
,然后按下键盘Ctrl+S
保存源文件 - 关闭VS Code
安装中文语言包 步骤2
安装中文语言包 步骤3 - 按下键盘
创建第一个C++项目(以下步骤除非有特殊说明,否则每次创建项目都要执行一次):
-
创建项目目录
cppdemo
,用于学习创建第一个C++项目,并进入cppdemo
目录中:mkdir ~/Projects/cppdemo && cd ~/Projects/cppdemo
-
创建配置目录:
mkdir ./.vscode
-
添加编译配置文件(输入以下内容然后按回车):
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
-
添加运行配置文件(输入以下内容然后按回车):
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
-
添加智能提示配置文件(输入以下内容然后按回车):
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
-
创建C++源文件
mycpp.cpp
(代码就是写在这个文件):touch mycpp.cpp
-
用Visual Studio Code打开当前工作环境(不要忽略最后的点哟):
code .
-
双击
mycpp.cpp
打开文件,第一次会提示安装C++插件,点击安装
,然后等待右下角消息安装完成(只需执行一次,以后都不需要再执行)。
-
双击打开
mycpp.cpp
,然后输入以下代码:
第一个 Hello world 程序#include <iostream> int main(void) { std::cout << "小古银的C++教程" << std::endl; return 0; }
-
按下
编译Ctrl+Shift+B
,然后选择build
,就会开始编译。 -
编译完后,在终端窗口按回车键关闭终端。
编译完按回车关闭终端 -
点击菜单栏中的
新建终端窗口终端
,然后点击新建终端
,就会打开终端
窗口。
-
然后在终端输入以下内容,就会运行程序:
./demoapp.out
-
运行程序将会显示
运行程序 输出“小古银的C++教程”小古银的C++教程
。
此时,第一个项目,也就是第一个程序就完成了。
网友评论