• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
Visual C++ Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
Visual C++ Developer Center > Visual C++ Forums > Visual C++ Language > error LNK2005: _main already defined
Ask a questionAsk a question
Search Forums:
  • Search Visual C++ Language Forum Search Visual C++ Language Forum
  • Search All Visual C++ Forums Search All Visual C++ Forums
  • Search All MSDN Forums Search All MSDN Forums
 

Answererror LNK2005: _main already defined

  • Tuesday, July 04, 2006 4:08 AMGKW82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    I recently installed VC++ 2005 EXP edition.

    1) I created a project name Radius and created two new items hello.cpp and area.cpp.

    2) area.cpp is the first item and hello.cpp is the second one.

    3) When I build the project Radius, I got the error

    LNK2005: _main already defined in area.obj

    Can you guide me to fix the compilation error.

    Thanks GKW82

    • ReplyReply
    • QuoteQuote
     

Answers

  • Tuesday, July 04, 2006 5:37 AMcythe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0

    This error is because of multiple definitions of main. You must have defined main in area.cpp as well as in hello.cpp. In C/C++ programs, you can write only one main per application.

    Remove one definition of main and it should work fine.

    Cheers

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Wednesday, July 05, 2006 11:26 PMGKW82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Yes. I defined main() in both. I removed the main from hello and I am now getting the error

    hello.cpp(4) : error C2447: '{' : missing function header (old-style formal list?)

    GKW82

    • ReplyReply
    • QuoteQuote
     
  • Thursday, July 06, 2006 1:39 AM郭郭 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Delete area.cpp and creat it.

    Do the thing above.

    • ReplyReply
    • QuoteQuote
     
  • Thursday, July 06, 2006 4:06 AMGKW82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Nope.. didn't work. Getting the same error.

    GKW82

    • ReplyReply
    • QuoteQuote
     
  • Thursday, July 06, 2006 5:24 AMSunil_D Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Pls. paste the hello.cpp code
    • ReplyReply
    • QuoteQuote
     
  • Thursday, July 06, 2006 6:24 AMcythe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    This is what MSDN says :

    Error Message

    '{' : missing function header (old-style formal list?)

    An open brace occurs at global scope without a corresponding function header.

    This error can be caused by an old-style C-language formal list.

    The following sample generates C2447:

    int c;
    {}       // C2447

    Check whether you are writing the open brace without the function name.

    Else post the code.

    • ReplyReply
    • QuoteQuote
     
  • Friday, July 07, 2006 4:49 PMGKW82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Here is the piece of code.

    1) radius.cpp

    #include <iostream>

    /* program to calculate radius of a circle */

    void main()

    {

    float area, radius;

    printf("Radius = ? ");

    scanf("%f", &radius);

    area = 3.14159 * radius * radius;

    printf("Area = %f", area);

    }

    2) hell.cpp

    include <iostream>

    {

    printf("Hello world\n");

    }

    c:\documents and settings\owner\my documents\visual studio 2005\projects\radius\radius\hello.cpp(4) : error C2447: '{' : missing function header (old-style formal list?)

    GKW82

    • ReplyReply
    • QuoteQuote
     
  • Friday, July 07, 2006 5:59 PMJames Kelly Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Try the following....

    1) radius.cpp

    #include <iostream>

    /* program to calculate radius of a circle */

    void main()

    {

       float area, radius;

       printf("Radius = ? ");

       scanf("%f", &radius);

       area = 3.14159 * radius * radius;

       printf("Area = %f", area);

    }

    2) hell.cpp

    include <iostream>

    void function_name()  // <---------------------------- did you miss this out?

    {

       printf("Hello world\n");

    }

    • ReplyReply
    • QuoteQuote
     
  • Friday, July 07, 2006 11:46 PMGKW82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    The whole problem starts when I include the function main() in the hello.cpp. I end up in getting the message

    error LNK2005: _main already defined in hello.obj

    GKW82.

     

     

    • ReplyReply
    • QuoteQuote
     
  • Saturday, July 08, 2006 3:37 PMJames Kelly Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    I think what you are trying to do is....


    1.) File: hello.cpp

    #include <IOSTREAM>
    #include <RADIUS.H>
    
    int main(int argc, char *argv[])
    {
          float area = get_area(5);
          printf("Area of circle with radius %d = %f\n", 5, area);
    }
    

    2.) File: radius.h

    float get_area(float radius);
    float get_circumference(float radius);
    

    3.) File: radius.cpp

    #include <RADIUS.H>
    
    float get_area(float radius)
    {
         return (3.14159 * radius * radius);
    }
    
    float get_circumference(float radius)
    {
         return (3.14159 * (radius * 2));
    }
    
    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement