我正在尝试设置VSCode,以便在Windows上构建和调试C。我安装了MinGW。
我试图为以下代码生成一个.exe文件:
#define USE_PTS true
#include "Cartography.h"
static Cartography cartography ;
static int nCartography = 0;
int main(void)
{
nCartography = loadCartography("map (1).txt", &cartography);
showCartography(cartography, nCartography);
interpreter(cartography, nCartography);
return 0;
}我还有另外两个文件Cartography.h和Cartography.c。如果我使用powershell终端运行以下命令,它将完美地生成一个.exe文件:
gcc -std=c11 -o Main Cartography.c Main.c -lm
但是,如果我尝试使用VSCode (使用Ctrl + Shift + B)构建它,它就不会识别其他文件:
> Executing task in folder Projeto2LAP: gcc -std=c11 -o Main Cartography.c Main.c -lm <
gcc.exe: error: Cartography.c: No such file or directory
gcc.exe: error: Main.c: No such file or directory
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.这是我的tasks.json文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "gcc -std=c11 -o Main Cartography.c Main.c -lm",
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}我遇到的另一个问题是当我试图使用内置的VSCode调试器时。我使用我提到的命令生成.exe文件,因此在当前文件夹中存在一个.exe文件。
(我还应该指出,我正在处理的文件夹只包含文件和.vscode文件夹,没有子文件夹或任何可能导致错误的内容)。
当我单击debug时,它会给出以下错误消息:
这是我的launch.json文件:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}我做错了什么?
发布于 2020-05-08 23:59:13
你的第一个问题:
当使用powershell命令时,它通过相对路径从当前的措辞目录中获取*.c文件。这是您在task命令中尝试执行的操作,但是您的CWD设置为错误的位置。在tasks.json中,您需要在gcc命令中指定文件的绝对路径,或者将CWD更改为*.c文件的目录。或者更好的是,VSC允许您设置"cwd": "${workspaceFolder}"。这意味着,如果您更改名称或做任何其他编辑,您的任务仍将工作。(要获得积分,可以将CWD设置为当前选定文件的封闭文件夹。)第一个问题第2部分:任务错误可能不是您。这似乎是一个已知的错误。指令这里来减轻它。
抱歉反应太晚了
我不知道为什么您会得到调试错误,所以我唯一的猜测是权限问题。调试器可能无法“查看”该目录的存在。确保它和它的任何父文件夹都没有不适当的紧身衣限制。
发布于 2020-05-11 07:43:40
与其在tasks.json中传递一个长字符串来启动GCC,不如考虑使用"args“列表变量,将每个arg放在列表的一个单独元素中。
假设源文件位于VSCode中打开的同一个目录中,前面提到的"${workspaceFolder}“宏将构造正确的路径。
否则,按照前面的建议提供源文件的绝对路径。
示例:
{
"label": "GCC Build Debug (64-bit)",
"type": "shell",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc",
"args": [
"--std=c11",
"-g",
"-lm",
"-o",
"${workspaceFolder}/Main"
"${workspaceFolder}/Main.c",
"${workspaceFolder}/Cartography.c"
],
"problemMatcher": [
"$msCompile"
]
},https://stackoverflow.com/questions/61686867
复制相似问题