vscode看代码
2019-11-21 本文已影响0人
技术笔记
发现vscode看代码真是舒服,但是用起来并不好用,在c、c++、java混合的代码环境下,导入代码目录,c和c++,代码跳转都没问题,java折腾多次都不行。
一个原因可能是和java环节变量有关,另一个原因可能是和android的build.gradle文件有关,看官网明确说不支持。
但是就一个c文件的编译,配置launch.json, tasks.json就花了好多时间,这种通过文本配置的太过灵活,一点也不好用。
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/shizh/awork/testcode/sample/c/sort/sort.out",
"args": [],
"stopAtEntry": false,
"cwd": "/home/shizh/awork/testcode/sample/c/sort",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb"
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb"
}
},
]
}
tasks.json
// 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 sample", // 任务的名字叫Build,注意是大小写区分的,等会在launch中调用这个名字
"type": "shell", // 任务执行的是shell命令,也可以是
"command": "/usr/bin/gcc", // 命令是gcc/g++
"args": [
"'-Wall'",
"-std=c99",
"'${file}'", // 当前文件名
"-o", // 对象名,不进行编译优化
"'${fileBasenameNoExtension}.out'", //当前文件名(去掉扩展名)
],
// 所以以上部分,就是在shell中执行(假设文件名为filename.cpp)
// g++ filename.cpp -o filename.out
"group": {
"kind": "build",
"isDefault": true
// 任务分组,因为是tasks而不是task,意味着可以连着执行很多任务
// 在build组的任务们,可以通过在Command Palette(F1) 输入run build task来运行
// 当然,如果任务分组是test,你就可以用run test task来运行
},
"problemMatcher": [
"$gcc" // 使用gcc捕获错误
],
}
]
}
发现没有删除编译好的文件时再次编译,会有这个报错:
> Executing task: /usr/bin/gcc '-Wall' -std=c99 '/home/shizh/awork/testcode/sample/c/sort/.vscode/tasks.json' -o 'tasks' <
/usr/bin/ld:/home/shizh/awork/testcode/sample/c/sort/.vscode/tasks.json: file format not recognized; treating as linker script
/usr/bin/ld:/home/shizh/awork/testcode/sample/c/sort/.vscode/tasks.json:1: syntax error
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
删除编译好的main后好了:
> Executing task: /usr/bin/gcc '-Wall' -std=c99 '/home/shizh/awork/testcode/sample/c/sort/main.c' -o 'main' <
Terminal will be reused by tasks, press any key to close it.
原因可能是我切换gcc,g++后需要删除
如果一直都是gcc就不会有问题。
最简单的办法
最简单最好理解的办法是写好makefile, 然后ctrl+f12, 进入terminal,直接make, 省事很多。