环境配置:Visual Studio Code 配置C/C++文件debug调试环境
昨天有伙伴私信我,为什么我用C语言写的hello world几行代码,在编译器里面报错了呢?
然后我让她截个图发我,却发现是她的VScode编译器没有配置好C/C++的编译环境,于是我给她解决完问题后,又想着可能还是有其他伙伴会遇到类似的问题,于是搜索一番之后将这篇我认为最细致的一篇文章转载分享给大家,希望能够帮助大家解决此类问题!

以下为正文:
vscode C/C++ Extension Pack 插件安装
下载直接安装 C/C++ Extension Pack 它包含了 vscode 编写 C/C++ 工程需要的插件,和以前比不需要一个个找了。

vscode windows 端 debug 配置
windows 端编译运行C/C++的程序需要一套集成开发环境,这里可以使用 MinGW https://nuwen.net/mingw.html ,选择自己需要的安装包安装即可,我就直接用推荐的。

window MinGW 环境安装
下载MinGW的安装包,安装即可(当然如果电脑上装着 Visual Studio,也可以用它的工具集配置环境)。
配置MinGW的bin目录到系统环境变量。

命令行gcc -v、g++ -v 验证环境变量是否配置成功

windows 端 C、CPP 单文件 debug 调试
安装好插件之后,先写一个简单的 cpp 文件
打个断点,直接运行debug,可以让编辑器自己去创建 lanch.json 和 task.json 配置文件

这里选择C++(GDB/LLDB)、不要用C++(windows),debug运行的是 windows 自带的 cmd

下一步 选择 g++.exe 如下截图;

记住不要用 C++(WIndows) 选项的话,会调用的是操作系统的cmd运行去运行程序,无法在vscode中正常debug,尝试了一下,截图如下。

注意生成的.vscode目录,其中配置文件 lanch.js 中的 preLaunchTask 值,需要和 tasks.json 中 的任务名称保持一致。对于不同的操作系统可能的其他环境,可以进行多任务配置。

C文件也可以调试。

我的windows端配置文件如下,大家可以参考。
代码文件中文目录文件名, 如何在 window 端正常调试
我是直接将 window 操作系统的编码改成 UTF-8,测试有效,不过之前磁盘中的的一些不是UTF-8编码的.txt 等文本文件,会有乱码的情况。win10和win11系统 UTF-8 的配置截图如下。
win10

win11

重启电脑后,就可以 debug 中文目录的单文件了。

vscode ubuntu端 debug配置
我这里的系统是Ubuntu20.04 版本,类linux系统当中配置vscode debug相对来说比window端要简单的多。
ubuntu C、CPP单文件调试
创建文件目录,写个简单测试程序,点击debug 按钮,可以让vscode 自己创建 launch.js 和 tasks.json
选择 C++(GDB/LLDB),参考截图如下

选择 gcc – 生成和调试活动文件,参考截图如下

成功进入断点。

我的ubuntu端配置文件如下,大家可以参考。
中文目录文件名, 在 ubuntu 端正常调试
在运行,Ubuntu调式中文目录,中文文件是没有问题的, locale 看一下当前系统的语言编码设置,默认是 UTF-8。

如果不行,可以修改默认配置 sudo vi /etc/default/locale,系统语言是中文,LANG=”zh_CN.UTF-8″,系统语言是英文的,LANG=“en_US.UTF-8”
完成修改之后可以, source /etc/default/locale 更新配置。截图如下


官方文档参考
vscode官方文档当中也有相关的配置,大家可以参考。 https://code.visualstudio.com/docs/cpp/config-mingw

希望能够帮到大家!
原文链接:https://www.cnblogs.com/elmluo/p/15933484.html?utm_source=tuicool&utm_medium=referral,作者:骆三疯
写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
:C语言编程学习基地
C语言零基础入门教程(83集全)
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
粉丝交流学习:

编程学习书籍分享:

2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/11967.html