当前位置:   article > 正文

如何使用vscode运行和调试c/c++程序_vscode 运行c++

vscode 运行c++

众所周知

vscode是个万金油,而且体型轻巧,拓展插件多,非常适合初学者编程

那么如何使用vscode进行c/c++程序的运行?

首先必须确保mingw64正确安装

通过以下链接下载解压到c盘根目录即可,然后添加修改path:“C:\mingw64\bin”

然后win+r 输入cmd 回车 输入gcc -v 出现版本号即代表path设置正确

mingw64下载,C语言学习,gcc-C/C++文档类资源-CSDN下载

然后打开vscode 安装插件code runner,c/c++这俩插件即可

然后打开文件-首选项-设置-搜索Run In Terminal,去勾选

然后搜索Code-runner: Executor Map,第一个选择在settings.json中编辑

  1. "c": "chcp 65001 && cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  2. "cpp": "chcp 65001 && cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

 如果找不到code-runner.executorMap,请在最后一行手打输入code-runner.executorMap,打一半软件会提示然后莫名其妙自动补全然后出现下面的一堆语言设置,然后在c和cpp行换成这俩行代码,加了chcp 65001确保code runner不会中文乱码错误发生

设置好以后就可以正常使用code runner即实现c/c++在vscode中的运行

接下来实现c/c++在vscode中的调试实现

首先你需要在vscode中打开一个文件夹,然后创建一个test.c文件,然后创建个.vscode文件夹,在里面创建四个文件

c_cpp_properties.json:将这段代码复制进去

  1. {
  2. "configurations": [
  3. {
  4. "name": "windows-gcc-x64",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "compilerPath": "C:/mingw64/bin/gcc.exe",
  9. "cStandard": "${default}",
  10. "cppStandard": "${default}",
  11. "intelliSenseMode": "windows-gcc-x64",
  12. "compilerArgs": []
  13. }
  14. ],
  15. "version": 4
  16. }

launch.json:复制粘贴

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "(gdb) Launch",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": true,
  14. "internalConsoleOptions": "neverOpen",
  15. "MIMode": "gdb",
  16. "miDebuggerPath": "gdb.exe",
  17. "setupCommands": [
  18. {
  19. "description": "Enable pretty-printing for gdb",
  20. "text": "-enable-pretty-printing",
  21. "ignoreFailures": false
  22. }
  23. ],
  24. "preLaunchTask": "Compile"
  25. },
  26. {
  27. "name": "C/C++ Runner: Debug Session",
  28. "type": "cppdbg",
  29. "request": "launch",
  30. "args": [
  31. ""
  32. ],
  33. "stopAtEntry": false,
  34. "cwd": "c:\\Users\\Rise\\Desktop\\c\\book",
  35. "environment": [],
  36. "program": "c:/Users/Rise/Desktop/c/book/build/Debug/outDebug",
  37. "internalConsoleOptions": "openOnSessionStart",
  38. "MIMode": "gdb",
  39. "miDebuggerPath": "gdb",
  40. "externalConsole": false,
  41. "setupCommands": [
  42. {
  43. "description": "Enable pretty-printing for gdb",
  44. "text": "-enable-pretty-printing",
  45. "ignoreFailures": true
  46. }
  47. ]
  48. }
  49. ]
  50. }

自行更改

"cwd": "c:\\Users\\Rise\\Desktop\\c\\book",

"program": "c:/Users/Rise/Desktop/c/book/build/Debug/outDebug",

改成自己的目录

tasks.json:复制粘贴

  1. // https://code.visualstudio.com/docs/editor/tasks
  2. {
  3. "version": "2.0.0",
  4. "tasks": [{
  5. "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
  6. "command": "gcc", // 要使用的编译器,C++用g++
  7. "args": [
  8. "${file}",
  9. "-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
  10. "${fileDirname}/${fileBasenameNoExtension}.exe",
  11. "-g", // 生成和调试有关的信息
  12. "-m64", // 不知为何有时会生成16位程序而无法运行,此条可强制生成64位的
  13. "-Wall", // 开启额外警告
  14. "-static-libgcc", // 静态链接libgcc,一般都会加上
  15. "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这条会导致Win下输出中文乱码;繁体系统改成BIG5
  16. "-D__USE_MINGW_ANSI_STDIO", // 用MinGW写C时留着,否则不需要,用于支持printf的%zd和%Lf等
  17. ], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
  18. "type": "process", // process是把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
  19. "group": {
  20. "kind": "build",
  21. "isDefault": true // 不为true时ctrl shift B就要手动选择了
  22. },
  23. "presentation": {
  24. "echo": true,
  25. "reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档,即使设为never,手动点进去还是可以看到
  26. "focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
  27. "panel": "shared" // 不同的文件的编译信息共享一个终端面板
  28. },
  29. "problemMatcher":"$gcc" // 捕捉编译时终端里的报错信息到问题面板中,修改代码后需要重新编译才会再次触发
  30. // 本来有Lint,再开problemMatcher就有双重报错,但MinGW的Lint效果实在太差了;用Clangd可以注释掉
  31. }]
  32. }

setting.json复制粘贴

  1. {
  2. "files.defaultLanguage": "c",
  3. "editor.formatOnType": true,
  4. "editor.suggest.snippetsPreventQuickSuggestions": false,
  5. "editor.acceptSuggestionOnEnter": "off",
  6. "code-runner.runInTerminal": true,
  7. "code-runner.executorMap": {
  8. "c": "gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO && &'./$fileNameWithoutExt.exe'",
  9. "cpp": "g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK && &'./$fileNameWithoutExt.exe'"
  10. },
  11. "code-runner.saveFileBeforeRun": true,
  12. "code-runner.preserveFocus": true,
  13. "code-runner.clearPreviousOutput": false,
  14. "code-runner.ignoreSelection": true,
  15. "code-runner.fileDirectoryAsCwd": true,
  16. "C_Cpp.clang_format_sortIncludes": true,
  17. "C_Cpp_Runner.cCompilerPath": "C:/mingw64/bin/gcc.exe",
  18. "C_Cpp_Runner.cppCompilerPath": "C:/mingw64/bin/g++.exe",
  19. "C_Cpp_Runner.debuggerPath": "gdb",
  20. "C_Cpp_Runner.cStandard": "",
  21. "C_Cpp_Runner.cppStandard": "",
  22. "C_Cpp_Runner.msvcBatchPath": "",
  23. "C_Cpp_Runner.warnings": [
  24. "-Wall",
  25. "-Wextra",
  26. "-Wpedantic"
  27. ],
  28. "C_Cpp_Runner.enableWarnings": true,
  29. "C_Cpp_Runner.warningsAsError": false,
  30. "C_Cpp_Runner.compilerArgs": [],
  31. "C_Cpp_Runner.linkerArgs": [],
  32. "C_Cpp_Runner.includePaths": [],
  33. "C_Cpp_Runner.includeSearch": [
  34. "*",
  35. "**/*"
  36. ],
  37. "C_Cpp_Runner.excludeSearch": [
  38. "**/build",
  39. "**/build/**",
  40. "**/.*",
  41. "**/.*/**",
  42. "**/.vscode",
  43. "**/.vscode/**"
  44. ]
  45. }

然后就可以在test.c中编辑代码按f5进行调试

上述gcc路径和工作区路径自行更改即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/65824
推荐阅读
相关标签
  

闽ICP备14008679号