0

我正在使用 VS Code 版本 1.62.0(用户设置)

我正在尝试在 C (test.c) 中运行一个基本程序:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Hello");
    return 0;
}

我试图通过单击播放符号 (CTRL+ALT+N) 来运行,但它在终端中变成这样:

PS C:\Users\X\Dropbox\My PC_X\Downloads> cd "c:\Users\X\Dropbox\My PC_X\Downloads\est\" ; if ($?) { g++ test.c *.c  -o test } ; if ($?) { .\test }
C:\Users\X\AppData\Local\Temp\ccyBS6yO.o:test.c:(.text+0x0): multiple definition of `main'
C:\Users\X\AppData\Local\Temp\ccwvYj0K.o:test.c:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

我不知道这些信息是否有帮助。供您参考,我使用的是 Windows 11(在我的情况下,我认为它真的坏了)。以前,我将 [桌面、文档、图片] 与 OneDrive 同步(OneDrive 成为我的本地文件夹)。但是前两天,我关闭了 Documents 的同步,一些文件完全在 OneDrive 中(不一定在我的本地文件夹中)[为了更清楚,看图]

破碎的窗户 11

这是我的 gcc 版本:

gcc.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 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.

这是我的task.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\MinGW\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: C:\\MinGW\\bin\\gcc.exe"
        }
    ]
}
4

2 回答 2

2

来自您的 IDE 配置,您正在尝试编译您的文件,然后是所有 .c 文件,因此包含第一个文件,因此它有 2 个 main,因为您编译了两次相同的文件

 g++ test.c *.c  -o test
于 2021-11-08T15:50:35.037 回答
1
if ($?) { g++ test.c *.c  -o test } 

*.c啊,所以在你运行扩展之后

g++ test.c test.c  -o test

所以,正如错误消息所说,你已经main定义了两次 - 在test.c. 所以,*.c从你的构建工作中删除。

于 2021-11-08T15:50:46.550 回答