ccompiler-errorsprogram-entry-pointmultiple-definition-error

multiple definition of main first defined here


I'm new to programming and currently I'm learning C programming. I'm writing codes on the code blocks and in it using GCC compiler. When I create a new project, (as you know it creates main.c file with it) and due to that I'm not able to compile another file in that project.

File 1:

 #include<stdio.h>

int main()
{
    int a,b,c,d;
    printf("Enter three numbers\n");
    scanf("%d%d%d",&a,&b,&c);
    d=a;
    if(b>d)
        d=b;
    if(c>d)
        d=c;
    printf("\n The maximum of three numbers is %d",d);

}

File 2: main.c

#include <stdio.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

When I compile the first programme, it shows the following error: multiple definition of 'main' first defined here

I've searched every where I could and I'm not able to solve this. In one of the answers here on stack overflow, someone had suggested to write this in (Project->Build options...->Linker settings (tab))

-Wl,--allow-multiple-definition

When I wrote it, there were no errors. But it wasn't able to run my File 1 and instead, it runs that main.c file. Even when I close the main.c file, it opens there again and runs main.c file which gives the output "Hello World!".

Initially when I was using code blocks there were no such errors. I don't know why this is happening and I've not much knowledge about compilers.


Solution

  • As noted in comments you can only have one main function.

    So when you start a new project you need to replace the main.c file with the main.c file you want to use. Or you can edit the 'hello world' main.c program.

    When you start a new project in code::blocks you can get a new directory with a simple program that prints 'Hello World'. This file is usually main.c. You need to edit this file or replace it. The reason that code::blocks puts this simple main.c program in the new project is so that you can compile it and test your system without having to write a new program.