【问题标题】:Fixing multiple definition of 'main' when running C in VS Code在 VS Code 中运行 C 时修复“main”的多个定义
【发布时间】:2021-12-21 10:58:39
【问题描述】:

我正在使用 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 中(不一定在我的本地文件夹中)[为了更清楚,看图]

这是我的 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"
        }
    ]
}

【问题讨论】:

  • 也是我这个目录下唯一的一个C文件

标签: c visual-studio-code gcc windows-11


【解决方案1】:
if ($?) { g++ test.c *.c  -o test } 

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

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

所以,正如错误消息所说,您已经定义了两次main - 在test.c 的两个副本中。因此,请从您的构建作业中删除 *.c

【讨论】:

  • 当我点击运行(Ctrl+Alt+N)时,这些“if ... g++ ... test”默认显示在终端中,所以每次我运行另一个程序时我应该删除“*.c”部分?或者我可以只编辑默认值以便它可以正常运行吗?怎么样?
  • 几乎可以肯定你已经在项目属性的某个地方进行了配置。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多