简体   繁体   中英

Fixing multiple definition of 'main' when running C in VS Code

I am using VS Code version 1.62.0 (user setup)

I am trying to run a basic program in C (test.c):

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

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

I am trying to run by clicking Play Sign (CTRL+ALT+N) but it turns like this in terminal:

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

I don't know if this information helps. For your information, I am using Windows 11 (Which in my case I think are really broken). Previously, I sync [Desktop, Documents, Picture] with OneDrive (OneDrive becomes my Local Folder). But two days ago, I turn off sync for Documents and some documents go fully in OneDrive (Not necessarily in my Local Folder) [To make it more clear, look at the picture]

破碎的 Windows 11

Here's my gcc version:

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.

Here's my 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"
        }
    ]
}

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

 g++ test.c *.c  -o test
if ($?) { g++ test.c *.c -o test }

Ah, so after expansion of *.c you run

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

so, as the error message says, you've got main defined twice - in the two copies of test.c . So, remove the *.c from your build job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM