经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
ubuntu下VS code如何调试C++代码
来源:cnblogs  作者:张杨  时间:2018/10/8 9:14:23  对本文有异议

最近开始使用Vs codel,真的方便,可以和git结合。下面总结一下如何调试程序,

我写了一个实例程序(不重要)

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. int main(void)
  8. {
  9. fstream iofile("test.txt");
  10. vector<string> strs(20);
  11. int i=0;
  12. if(!iofile){
  13. cerr<<"open file failed"<<endl;
  14. }else{
  15. while(iofile>>strs[i++]);
  16. }
  17. for(int j=0;j<i-1;j++)
  18. {
  19. cout<<strs[j]<<endl;
  20. }
  21. // cout<<endl;
  22. cout<<"after sort"<<endl;
  23. cout<<*(strs.begin())<<endl;
  24. sort(strs.begin(),strs.begin()+i-1);
  25. cout<<"i:"<<i<<" "<<strs[1]<<endl;
  26. for(int j=0;j<i;j++)
  27. {
  28. cout<<strs[j]<<" ";
  29. }
  30. cout<<endl;
  31. return 0;
  32. }

这个时候,我们按F5,发现不能运行,它提示需要一个Launch.json文件,OK,这是一个启动文件,我们来配置它。

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "(gdb) Launch",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${workspaceFolder}/build",
  12. "preLaunchTask": "build",
  13. "args": [],
  14. "stopAtEntry": false,
  15. "cwd": "${workspaceFolder}",
  16. "environment": [],
  17. "externalConsole": true,
  18. "MIMode": "gdb",
  19. "setupCommands": [
  20. {
  21. "description": "Enable pretty-printing for gdb",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. }
  25. ]
  26. }
  27. ]
  28. }

注意,这里需要修改的部分主要是program那一行,仅需修改为自己编译后产生的文件名,不如g++ -g 1.7.cpp -o build,所以这里我就取了build这个名字。

还有,就是要添加preLaunchTask这一行,名字与下面的task要对应。

这里,它还需要一个task.json文件,配置如下,

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "label": "build",
  8. "type": "shell",
  9. "command": "g++",
  10. "args": [
  11. "-g",
  12. "${workspaceFolder}/chapter01/1.7.cpp",
  13. "-o",
  14. "build"
  15. ]
  16. }
  17. ]
  18. }

大家可以看到,这里只需要将label的值要与上面的preLaunchTask相对应,其他的就是命令行部分,完成这个文件后,我们ctrl+shift+b,这是会执行task。

之后,我们打开main文件,设置断点,F5即可开始调试代码。

 

关于Launch.json:

官方是这样说的:However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details. VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings.但是,对于大多数调试方案,创建启动配置文件是有益的,因为它允许您配置和保存调试设置详细信息。 VS Code将配置信息保存在位于工作区(项目根文件夹)的.vscode文件夹或用户设置或工作区设置中的launch.json文件中。

个人感觉可能是项目比较简单所以看不来它的好处。

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号