在windows下使用vscode时的问题及解决方法
1 VScode常用快捷键
发现在win10下这个好用,在ubuntu下不好使
Alt+Shift + ⬆ : 在当前行代码上面一行复制当前行代码
Alt+Shift + ⬇ : 在当前行代码下面一行复制当前行代码
2 VS Code 中文注释显示乱码怎么办?
3 Visual Studio Code 如何编写运行 C、C++ 程序?
在windows平台下使用vscode开发C++的话 需要单独下载MinGW编译器具体方法见链接
注意:在添加环境变量的时候,是在用户变量中的PATH中添加
图片链接参考:
4 多文件编译
将vscode文件夹下的tasks.json文件更改下就可以了,将args中的-g下添加需要编译的其他cpp文件名称即可。注意不要添加将要运行编译的那个cpp文件,否则会提示main函数重复。
上述多文件编译操作是在windows下开发c++程序时需要的,在开发ROS的时候暂时还没用到。先记录下。
// https://code.visualstudio.com/docs/editor/tasks
{
"version": "2.0.0",
"tasks": [{
"label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
"command": "g++", // 要使用的编译器,C++用g++
"args": [
"${file}",
"-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g", // 生成和调试有关的信息
// "${fileDirname}/circle_point.cpp", //多文件编译添加的
"${fileDirname}/circle.cpp", //多文件编译添加的
"${fileDirname}/point.cpp", //多文件编译添加的
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接libgcc,一般都会加上
"-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这一条会导致Win下输出中文乱码
// "-std=c11", // C++最新标准为c++17,或根据自己的需要进行修改
], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
"type": "process", // process是vsc把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
"group": {
"kind": "build",
"isDefault": true // 不为true时ctrl shift B就要手动选择了
},
"presentation": {
"echo": true,
"reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档
"focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
// "problemMatcher":"$gcc" // 此选项可以捕捉编译时终端里的报错信息;但因为有Lint,再开这个可能有双重报错
}]
}
如果使用code-runner的话,将"code-runner.executorMap"
里面的cpp设置由$fileName
更改位*.cpp
即可。
{
"files.defaultLanguage": "c++", // ctrl+N新建文件后默认的语言
"editor.formatOnType": true, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
"editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
"editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
// "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
"code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
"code-runner.executorMap": {
"c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
//"cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && '$dir$fileNameWithoutExt'"
// "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && $dir$fileNameWithoutExt",
// "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && $dir$fileNameWithoutExt"
}, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),PS和bash也能用,但文件名中有空格时无法运行
"code-runner.saveFileBeforeRun": true, // run code前保存
"code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
"code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
"code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
"C_Cpp.clang_format_sortIncludes": true,
"files.associations": {
"iostream": "cpp",
"istream": "cpp",
"xstring": "cpp",
"cmath": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
}, // 格式化时调整include的顺序(按字母排序)
}
在Linux下配置vscode环境
注意事项:
- task是负责编译部分的操作,相当于命令版本的g++操作,把参数之类的设置都在这个json中完成
- launch是负责运行部分的操作。
这两者需要结合着使用,注意在task.json 中的label设置的名字需要和 launch.json文件中preLaunchTask设置的名字相同。这样两个json才能关联起来。否则在调试的时候就会提示你program参数中写的文件不存在。
task.json
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
launch.json
{
// 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
// "program": "${fileDirname}/${fileBeasenameNoExtension}",
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/eigenMatrix",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file"
},
]
}