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

Time:11-09

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]

Broken 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"
        }
    ]
}

CodePudding user response:

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.

CodePudding user response:

that came from your config of the IDE, you are trying to compile your file and then all .c files and so the first one included so it have 2 main since you compile twice the same file

 g   test.c *.c  -o test
  • Related