Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2005: _main already defined in hold.obj

Tags:

c++

c

visual-c++

Hi Please i have browsed all same error that i got but I didnt get solving for my problem, so I am using MS VC++ 2010 and i have two files a.c and b.c, each one works no error alone and each one has a simple code and clear. But when i use them to gather shows this error **error LNK2005: _main already defined in a.c ** this same error shows on Code block IED. I think that refer to using main function twice. Now how can i use one main function for both file

Code file a.c

#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();
}

Code file b.c

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

main()
{
    int x=10;

    printf("%d", x);
    getch();
}    
like image 758
Basil Avatar asked Oct 27 '14 08:10

Basil


1 Answers

It is not possible to have two main functions, a program starts running in only 1 main function. You could rename the main functions, and create one main function that calls them both.

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();
}

Code file b.c

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

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

void b_main()
{
    int x=10;

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

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

like image 83
Scott Langham Avatar answered Nov 17 '22 22:11

Scott Langham