Error LNK2005: _main is already defined in hold.obj

Hi, I looked at all the same error as I did, but I didn't get a solution for my problem, so I am using MS VC++ 2010

I also have two files a.c and b.c,

, each of them doesn't work without errors, and each of them has a simple code and cleans up, But when I am using them to collect shows this error **error LNK2005: _main already defined in a.c **

, this same error is displayed in the IED code block. I think this refers to using the main function twice. Now how can I use one main function for a file

Ac file code

#include<stdio.h>
#include<conio.h>

main()
{
    int a =9;
    if(a==7)
    {
        puts("This is number seven ");
    }
    else
    {
        puts("This isn't number seven ");
    }

    getch();
}

      

Bc file code

#include<stdio.h>
#include<conio.h>

main()
{
    int x=10;

    printf("%d", x);
    getch();
}    

      

+3


source to share


1 answer


It is not possible to have two main functions, the program only runs one main function. You can rename the main functions and create one main function that calls both of them.

Code file a.c

#include <stdio.h>
#include <conio.h>

void a_main()
{
    int a =9;
    if(a==7)
    {
        puts("This is number seven ");
    }
    else
    {
        puts("This isn't number seven ");
    }


    getch();
}

      

Bc file code



#include <stdio.h>
#include <conio.h>

void main()
{
   a_main();
   b_main();
}

void b_main()
{
    int x=10;

    printf("%d", x);
    getch();
}

      

Note that it is good practice to neatly name your functions so that the names describe what they do. For example, in this example, you can call a_main PrintIs7OrNot and b_main Print10.

+6


source







All Articles