Error: Multiple definition of '_main'...

This is a discussion on Error: Multiple definition of '_main'... within the C Programming forums, part of the General Programming Boards category; HI, As u may think, I am new to C programing and as I was making programs in C, my ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Arrow Error: Multiple definition of '_main'...

    HI,
    As u may think, I am new to C programing and as I was making programs in C, my first program was successfully compiled while all the programs that followed contained load of errors; the main being
    <<Error: Multiple definition of '_main'>>
    I think that the first program that was compiled has a 'main' that links with all the other programs that I want to create.
    Can anyone tell me what is the way out for creating other programs without this error??
    By the way, I am using DJGPP's C compiler on a win98 PC.
    Is there something that I have to change the properties anywhere??
    Any help appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can only have one function called 'main' in your program. That's what it's telling you. You can't link multiple stand alone (meaning they each have a 'main') programs into a single one. Just like Highlander, there can be only one.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Re: Error: Multiple definition of '_main'...

    As I am new to C programing, I dont know about the linking of the *.c files. What I mean to say is that first I made my first C program named 'HelloWorld.c' and next I wanted to make another C program relating to addition of 2 numbers <add.c>.
    Now, HelloWorld.c runs perfectly without errors, but the other programs that I want to write, are not compiled properly and are with loads of errors that I don't think have any.
    i.e. add.c when compiled says:::
    Error: Multiple definition of '_main' with other errors following it.
    When I point to this error, the DJGPP program takes me back to the 'main' statement I wrote in HelloWorld.c .

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Well, this is specific to your compiler.

    Evidently, DJGPP (which I've never used) thinks you're trying to compile "add.c" and "HelloWorld.c" together as one program, when you clearly want them to be separate programs.

    If DJGPP is an IDE, make sure you clear everything and create a new project (or whatever appropriate).

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Ok, I think I should try Turbo C to make my programs work.
    Thanks a lot.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    djgpp is a newer compiler than Turbo C. Just compile one program at a time like cwr suggested. You'll have the same problem with Turbo C (assuming you're using the IDE) if you just keep adding new files to the same project.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Ok, I think I should try Turbo C to make my programs work.
    1. this won't solve the problem of having two mains
    2. it's a step back to the stone age.

    You're going to have to call one of the functions something else, and call it from the single main() which remains.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    I think I found the problem.
    In my computer, DJGPP considers all the .c programs written one after the other are read as a single project that does not require the repetitive codes such as 'main'...
    However, I do not know how to make single programs work as their own entity and not as a whole project.
    So, now I am using Turbo C which does not have the same facility (/annoyance).
    Thanks for your replies.
    Anyways, for me as a beginner of C language, Turbo C is a CLASSIC compiler, though old/ outdated; it works for ME.
    [B]Those who proclaim themselves to be the God of C are often imperfect themselves. I hope you don't mind this and agree...
    GOD BLESS ALL... :-)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I doubt that turbo c doesn't have the option of compiling/linking multiple files.

  10. #10
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by thunderdome
    I think I found the problem.
    In my computer, DJGPP considers all the .c programs written one after the other are read as a single project that does not require the repetitive codes such as 'main'...
    However, I do not know how to make single programs work as their own entity and not as a whole project.
    I think it would be better for you to learn your tools rather than change them at the first bit of confusion. This just doesn't seem an auspicious start to your coding efforts. Can't you just look under "File" for "New Project" or something like that? It's probably that simple.

    So, now I am using Turbo C which does not have the same facility (/annoyance).
    Thanks for your replies.
    Anyways, for me as a beginner of C language, Turbo C is a CLASSIC compiler, though old/ outdated; it works for ME.
    I think "classic" is too generous a term -- the last release of Turbo C was over 15 years ago, wasn't it? You don't need a fancy ide (I use gvim and an xterm myself) but you really should learn C with a modern compiler.

  11. #11
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by thunderdome
    I think I found the problem.
    In my computer, DJGPP considers all the .c programs written one after the other are read as a single project that does not require the repetitive codes such as 'main'...
    However, I do not know how to make single programs work as their own entity and not as a whole project.
    So, now I am using Turbo C which does not have the same facility (/annoyance).
    Thanks for your replies.
    Anyways, for me as a beginner of C language, Turbo C is a CLASSIC compiler, though old/ outdated; it works for ME.
    [B]Those who proclaim themselves to be the God of C are often imperfect themselves. I hope you don't mind this and agree...
    GOD BLESS ALL... :-)

    "Classic" is nice if you're talking cars, movies, and perhaps television programs.

    It's NOT nice if you're talking compilers. A compiler that has not had a release in 15 years may not support certain things, and may allow you to do heinous things in your code, that a good, up-to-date compiler will catch.

    The compiler is telling you things for a reason. While it is annoying, even maddening, at times, if you dig down and find out WHY the compiler is gakking on you, and what you need to do to fix it, you'll become a better programmer.

    As for Salem, from the posts I've read, he's perfectly capable of calling himself God of the C..... while it's probably tongue in cheek, he certainly knows his stuff.....

    So -- pull out your original compiler, get your favorite beverage, and sit down and dig in!

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Evidently, DJGPP (which I've never used) thinks you're trying to compile "add.c" and "HelloWorld.c" together as one program, when you clearly want them to be separate programs.

    If DJGPP is an IDE, make sure you clear everything and create a new project (or whatever appropriate).
    DJGPP compiles all open files into one exe, even if you don't have a project open. Close your other file[s] and it will compile just fine.

    DJGPP is much better than Turbo C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Thanks all.
    Sorry To Salem If He Did Mind What I Said. I think I was rude in saying it so badly.

    Can anyone tell me if there is any compiler which can give us the time to look at the output rather than typing getch(); OR other methods like printf(); ? Its b'coz the book that I refer doesn't have getch(); in any of its programs. So I think there must be such a compiler.

    BTW, I am using Win98 and have now tried both gvim and Borland. (gvim seems to be rather hard to use for me !) Yes, the Borland version is good.

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You ever heard of a help file?

  15. #15
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Bubba
    You ever heard of a help file?
    Or was we sometimes mutter in the office: "RTFM"


    I'm going to take a stab at what thunderdome is getting at. To me it sounds like he needs/wants/would like a debugger. The ability to step through the code as it executes.

    If you're running the code in "real time", and want to see what is going on, you do need to use print statements (printf to stdout, fprintf to a trace file etc). There's no compiler functionality that will do that for you.

    It might be a part of your IDE, but I'm not all that familiar with IDEs on Windows (although I might have to become more familiar in the near future).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21