Ubuntu快速搭建C++开发环境(VS Code编辑器)
以下安装的是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:
1
sudo apt-get -y install software-properties-common
- 添加PPA到库并更新(会提示按回车继续执行,此时按回车即可):
1
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
- 更新软件信息:
1
sudo apt-get update
- 安装g++:
1
sudo apt-get -y install g++-8
- 将g++指向g++-8:
1
sudo ln -sf /usr/bin/g++-8 /usr/bin/g++
- 显示g++版本号,如果正常显示版本号意味着安装成功:
1
g++ --version
安装Visual Studio Code
- 安装libgconf库:
1
sudo apt-get install -y libgconf-2-4
- 安装git:
1
sudo apt-get -y install git
- 添加PPA到库并更新(会提示按回车继续执行,此时按回车即可):
1
sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
- 更新软件信息:
1
sudo apt-get update
- 安装ubuntu-make:
1
sudo apt-get -y install ubuntu-make
- 通过ubuntu-make安装Visual Studio Code;过程中会询问安装路径,此时不需要修改直接按回车即可;然后会询问是否接受协议,此时输入a然后回车即可:
1
sudo umake ide visual-studio-code
- 创建软链接到程序目录下:
1
sudo ln -sf `env | grep ^HOME= | cut -c 6-`/.local/share/umake/ide/visual-studio-code/bin/code /usr/bin/code
- 创建项目目录:
1
mkdir ~/Projects
- 显示Visual Studio Code版本号,如果正常显示版本号意味着安装成功:
1
code --version
- 运行VS Code:
1
code
- 安装中文语言包:
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创建第一个C++项目(以下步骤除非有特殊说明,否则每次创建项目都要执行一次):
- 创建项目目录cppdemo,用于学习创建第一个C++项目,并进入cppdemo目录中:
1
mkdir ~/Projects/cppdemo && cd ~/Projects/cppdemo
- 创建配置目录:
1
mkdir ./.vscode
- 添加编译配置文件(输入以下内容然后按回车):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27cat > .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 - 添加运行配置文件(输入以下内容然后按回车):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27cat > .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 - 添加智能提示配置文件(输入以下内容然后按回车):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79if [ `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(代码就是写在这个文件):
1
touch mycpp.cpp
- 用Visual Studio Code打开当前工作环境(不要忽略最后的点哟):
1
code .
- 双击mycpp.cpp打开文件,第一次会提示安装C++插件,点击安装,然后等待右下角消息安装完成(只需执行一次,以后都不需要再执行)。
- 双击打开mycpp.cpp,然后输入以下代码:
1
2
3
4
5
6
7#include <iostream>
int main(void)
{
std::cout << "小古银的C++教程" << std::endl;
return 0;
} - 按下Ctrl+Shift+B,然后选择build,就会开始编译。
- 编译完后,在终端窗口按回车键关闭终端。
- 点击菜单栏中的终端,然后点击新建终端,就会打开终端窗口。
- 然后在终端输入以下内容,就会运行程序:
1
./demoapp.out
- 运行程序将会显示小古银的C++教程。
此时,第一个项目,也就是第一个程序就完成了。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Yuan!
评论