提问人:Riya Sharma 提问时间:11/6/2023 最后编辑:ks1322Riya Sharma 更新时间:11/7/2023 访问量:56
如何在VS Code中添加MinGW编译器以运行C prog
how to add mingw compiler in vs code to run c prog
问:
我收到错误,指出启动程序不存在
我安装了 Windows 所需的 C 编译器 mingw,编译时仍然面临错误。 如何运行 C 程序? 我已经安装了与 c、c 扩展相关的所有扩展,实际上我也安装了 code runner。 早些时候,它在顶部显示了一个运行按钮,但现在它没有显示,而是显示要添加的配置。
答:
您不需要代码运行程序。您可以配置 VSCode 以允许您运行代码并以更好的方式对其进行调试。
检查 GCC
转到终端并输入它。gcc --version
如果你得到这样的结果:
gcc (Rev1, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
这意味着 GCC 已安装,应该被 VSCode 找到。
tasks.json
必须为 tasks.json 提供正确的路径才能执行生成和运行。下面是一个示例
{
"version": "2.0.0",
"tasks" :
[
{
"label" : "build release",
"type" : "shell",
"command": "gcc main.c -O2 -o ${workspaceFolderBasename}.exe"
},
{
"label" : "build debug",
"type" : "shell",
"command": "gcc main.c -g3 -o ${workspaceFolderBasename}.exe"
},
{
"label" : "run_executable",
"type" : "shell",
"command": "./${workspaceFolderBasename}.exe"
}
]
}
这假定所需的可执行文件名称与文件夹名称相同。例如,如果文件夹名称为 ,则可执行文件将变为 .当然,您可以通过替换 .my_c_project
my_c_project.exe
${workspaceFolderBasename}
要运行它或调试它,您需要 .要检查它,请在终端中使用,它应该为您提供此输出。GDB
gdb --version
GNU gdb (GDB) 13.2
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
此外,您还必须配置启动配置。
launch.json文件
下面是一个示例
{
"configurations": [
{
"name" : "Debug Config",
"type" : "cppdbg",
"request" : "launch",
"program" : "${workspaceRoot}/${workspaceFolderBasename}.exe",
"args" : [],
"preLaunchTask" : "build debug",
"stopAtEntry" : true,
"cwd" : "${workspaceRoot}",
"environment" : [],
"externalConsole": false,
"MIMode" : "gdb",
}
]
}
其中第一个重要的部分是参数,它应该指向构建后创建的程序。您可以看到 和 任务都在基本文件夹(放置 main.c 的位置)创建可执行文件。因此,的属性也指向相同的可执行文件。program
build debug
build release
program
launch.json
第二个重要位是参数,它告诉调试器在开始调试之前执行任务。它已设置为任务,因此每次调试时,它都会重新生成,以便使用最新的代码。preLaunchTask
build debug
设置好这些后,只需转到调试选项卡,然后单击右上角的绿色调试符号即可。
它将开始调试。中提琴!
评论
下一个:为什么我无法访问 C 中的文件
评论