在VMware中Ubuntu18中使用vscode编写c++程序
一、下载Ubuntu镜像源
二、下载vmtools
下载vmtools可以使ubuntu桌面占满整个vmware界面的屏幕,还可以与windows桌面交互使用文本/文件复制粘贴的功能,例如虚拟机科学上网不太方便,可以用windows科学上网下载完后复制到虚拟机中来。
vmtools无法实现复制粘贴
- 卸载已经下载好的vmtools
sudo apt-get autoremove open-vm-tools
- 重新安装vmtools
sudo apt-get install open-vm-tools
- 安装vmtools-desktop
sudo apt-get install open-vm-tools-desktop
- 重启Ubuntu
reboot
三、切换清华镜像源
- 打开终端,进入镜像源文件
cd /etc/apt
- 备份Ubuntu系统原有的镜像源
sudo cp /etc/apt/sources.list /etc/apt/sources.backup.list
这样如果后面想改回来Ubuntu系统源的的话直接使用下面的就能改回默认源了
sudo cp /etc/apt/sources.backup.list /etc/apt/sources.list
- 然后使用超级用户模式使用gedit打开sources.list
sudo gedit /etc/apt/sources.list
- 替换为以下内容,清华大学镜像源网站
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
- 更新清华源对应的包
sudo apt-get update
四、下载VScode
这个直接在Ubuntu软件中下载就好了
五、下载c++必要的文件
好像下载g++的时候gcc和gdb也会一起下载,反正都下载一下确认一下
sudo apt install g++
sudo apt install gcc
sudo apt install gdb
sudo apt install cmake
六、vscode操作
新建文件夹
再选择用vscode打开,并安装c++扩展
新建hello.cpp文件
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
return 0;
}
七、配置三个JSON文件
再按F1,编辑配置
会自动生成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
}
再按调试按钮,
会自动生成task.json文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
再在调试界面先点运行和调试,再创建json文件
做一些小小的修改,注意其中task中的label和launch中的preLaunchTask内容一致
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ 生成活动文件"
}
]
}
接下来就可以愉快地用vscode写c++啦
八、可能遇到的问题
(1) vscode运行的时候字符间距很宽
左下角管理-》设置-》搜索 Integrated:Font Family
将其值改为monospace
(2) 多个cpp之间调用
将task.json中的"g"内容改为${fileDirname}/*.cpp
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
// "-g","${file}",
"-g","${fileDirname}/*.cpp",
"-o","${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
(3) 外部终端显示
launch.json文件中的externalConsole改为true
(4) 运行c++时输出[1] + Done “/usr/bin/gdb”
launch.json文件的configuration中添加
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi",
有别的问题可以私信我处理