纯小白 尝试用vs code debug c/c++程序

2024-02-29

vs code太高级了,搞了半天也没明白它的工作原因。现在hello world的门槛都这么高了吗?

我使用的vs code版本:version 1.87.0

 第一步:

新建一个目录hello作为工程目录。File -> Open Folder... 打开hello目录。

在新工程根目录下新建文件main.c   File -> New File...

1
2
3
4
5
6
#include <stdio.h>
 
int main(){
    printf("sdf");
    return 3;
}

如果只是想看运行结果,直接在main.c文件中右击选中Run Code就可以看到运行结果

如果想debug打断点,继续往下看。

然后直接点运行.

我这里的报错是:

 这时候系统已经帮我们生成

c_cpp_properties.json

launch.json

settings.json

3个配置文件了。直接点开Open 'launch.json'文件。

 Terminal -> Configure Default Build Task 生成默认的tasks.json文件。

 运行task任务

报错: Cannot build and debug because the active file is not a C or C++ source file.

未发现c源文件。

 

 我就修改了两处${file}换成了main.c   options下的cwd换成了当前工程目录,然后点保存(很重要),再次运行任务task成功了。

 记住这个生成可执行文件的路径是:d:/c/hello/build/Debug/outDebug

然后回到launch.json文件

把里面的

   "program": "d:/c/hello/build/Debug/outDebug",

改成  

      "program": "d:/c/hello/.vscode/tasks.exe",
就可以了。
在main.c里面打一个断点,可以开始debug了。
 

 ---------------------------------------------------------------------------------------------------

中间出现一个小插曲:

明面上是说MiDebuggerPath的参数不对。看默认是

     "miDebuggerPath": "gdb",
这个gdb.exe就是c的debug工具。后来机缘巧合,我才发现,这个gdb.exe本来应该是在MinGW/bin下面的,我发现我的目录没有,果然重新下载一个MinGW,然后最新的MinGW里面的bin下面是有gdb.exe
,问题完美解决。
给一下MinGw的下载地址:

链接:https://pan.baidu.com/s/10BdhTWWS0VO2LDf9_llQrg
提取码:qkpw

 

 因为胳膊拧不过大腿,vs code里面的launch.json中的program总是被系统自动改成build/Debug/outDebug这个目录了,

为什么了统了,我们也修改一下tasks.json里面的生成路径吧

 

 

有时候,为了编译和运行一步搞定,可以把launch.json中指定任务

 

"preLaunchTask": "C/C++: gcc.exe build active file"
preLaunchTask的值,对应tasks.json中的label的值。

2024-07-02

Starting build...
cmd /c chcp 65001>nul && D:/soft/MinGW/bin/gcc.exe -fdiagnostics-color=always -g -I D:\c\ffmpeg_win\include D:\c\ffmpeg_win/*.c -LD:\c\ffmpeg_win\dll -lavutil-59 -lswscale-8 -lavcodec-61 -lavdevice-61 -lavfilter-10 -lavformat-61 -lpostproc-58 -lswresample-5 -o D:\c\ffmpeg_win\main.exe
C:\Users\���\AppData\Local\Temp\cc8qKxs3.o: In function `main_C':
D:/c/ffmpeg_win/main.c:8: undefined reference to `main_ffprobe'
D:/soft/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

原来这个错误是编译软件找不到main方法,加一个main方法就好。

--------------------------------------------------------------------------------------------------------------------------------------

如果重复引用同一个文件,会报错: multiple definition of `main'

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc0000135.

这个报错,是因为我在编译task的时候, 有把dll动态库文件放到dll目录下,但是在执行exe的时候,exe只让当前目录下的dll才有效。解决办法。

在launch.json中加environment字段,里面填path地址

 

 

posted on   angelshelter  阅读(1464)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
历史上的今天:
2020-02-29 切WKWebView

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示