最近刚过安装了中文版的ubuntu18.04.1,安装完以后想在ubuntu上安装vscode作c/c++的开发调试,踩了很多坑,在此记录一下,但愿你们在这条路上不要再踩一样的坑。
安装vscode很简单,只须要一个命令便可搞定:
$ sudo apt-get install visual-studio-code
注:若是须要卸载,可以使用 $ sudo apt-get remove codelinux
安装成功后,会在菜单栏上出现vscode的标签,若是没有,则能够在terminal中使用命令./code打开vscodec++
安装完vscode后,用vscode打开代码工程目录,并根据提示安装c/c++插件后,发现鼠标没法跟踪函数和成员变量的定义,即control+鼠标单击(或鼠标右键)->Go to Definition时,提示no definition found for *。被这个莫名其妙的问题搞得一头雾水,百思不得其解。以前ubuntu16.04.4一直用的好好的,为何到了18.04.1就找不到函数定义了。
后来仔细看提示才发现,代码路径里面的文件夹名称桌面是中文名(代码放在桌面目录下),会不会是由于这个问题才致使vscode没法跟踪函数定义呢?因而将ubuntu系统切成英文系统,切换方法以下:
1.打开系统菜单中的设置-》Region & Language
将language/语言 从中文改成English(United States)
将Formats/格式 从中国改成United States
而后重启系统
2.重启以后发现home目录下除了中文的桌面目录以外,还多了一个Desktop目录,因而将中文桌面目录下的全部文件夹剪切到Desktop目录下,并检查工程代码目录下是否还有其余中文字符,有的话继续改为英文。
3.使用vscode打开工程文件夹目录,而后再control+鼠标单击-》Go to Definition跟踪函数定义,函数已经自动跳转到函数的定义页面。问题成功解决。shell
准备源码main.cpp,代码以下:json
#include <stdio.h> int main() { printf("vscode test debug\n"); int a = 3; int b = 2; int c = a*b; printf("a+b=%d\n",c); getchar(); return 0; }
为3.1的测试代码准备一个makefile文件,内容以下:ubuntu
TARGETNAME = build all:$(TARGETNAME) main.o:main.cpp g++ -g -O0 -Wall -fPIC -c $^ $(TARGETNAME):main.o g++ -o $@ $^ .PHONY:clean clean: rm -f $(TARGETNAME) main.o $ g++ -g -c test.cpp $ g++ -o test test.o
在terminal中make会生成build可执行文件windows
使用vscode打开test.cpp所在的文件夹目录,按F5,弹出选择调试环境对话框(Select Environment),从对话框的下拉菜单中选择C++(GDB/LLDB),以下图所示:
选择后C++(GDB/LLDB)程序自动生成launch.json文件,以下所示:visual-studio-code
{ // 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": "enter program name, for example ${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
1)、将数组
"program": "enter program name, for example ${workspaceFolder}/a.out",
改成:函数
"program": "${workspaceFolder}/test",
2)、将visual-studio
"externalConsole": true,
改成:
"externalConsole": false,
3)、若是存在程序启动参数,则将
"args": [],
改成:
"args": ["arg1","arg2", "arg3"],
进入.vscode目录(隐藏文件),建立tasks.json文件。
内容以下所示:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "make", //"args":["-g", "${workspaceRoot}/main.cpp","-o","build"], "problemMatcher": [ "$gcc" ] } ] }
保存后按ctrl+shift+B进行编译。编译成功后会在当前目录下生成名为build的可执行文件。而后在代码中设置断点,按F5便可进行调试代码。
进入.vscode目录(隐藏文件),建立tasks.json文件。
内容以下所示:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args":["-g", "${workspaceRoot}/main.cpp","-o","build"], "problemMatcher": [ "$gcc" ] } ] }
保存后按ctrl+shift+B进行编译,编译成功后便可进行调试。
用vscode打开源码,使用F9在须要调试的地方设置断点,按F5运行程序,即可以开始linux下程序的调试,调试方法与windows的vs开发环境几乎彻底同样。
一、vscode的代码路径不能有中文,不然会出现没法跟踪代码定义的问题;二、"externalConsole": 应该为false,不然没法进入调试;fu三、args为一个数组,而不是一个字符串四、tasks.json的label为编译目标程序名称,必须与launch.json的"program": " ${workspaceFolder}/$(programname)",中的$(programname)相同,不然调试将出问题。五、"command": "g++"为使用g++编译,对于c++源码,"gcc"为使用gcc编译,对应c源码,"make"对应makefile文件编译,源码工程目录下必须有对应makefile工程