Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
hello i keep getting this error from gcc: "first.cpp:9: error: `main' must return `int'" when i try to compile my C++ program. what does this error mean and how do ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2004
    Posts
    50

    main must return int


    hello i keep getting this error from gcc: "first.cpp:9: error: `main' must return `int'" when i try to compile my C++ program.
    what does this error mean and how do i fix it. this is my first time using C++.

    heres my C++ code:
    Code:
    #include <iostream.h>
    
    void main&#40;&#41;
    &#123;
    	// Write "Using C++" to the screen
    	cout << "Using C++\n";
    &#125;

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Don't write void main write int main

  3. #3
    Just Joined!
    Join Date
    Dec 2004
    Posts
    50
    ok, thanks
    i figured out the problem and that was part of it
    heres what i did wrong (for future reference i guess?)
    1. the extension should be .C not .c or .cpp
    2. use g++ compiler not gcc
    3. use int main() not void main()

  4. #4
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by Sygnus X1
    ok, thanks
    i figured out the problem and that was part of it
    heres what i did wrong (for future reference i guess?)
    1. the extension should be .C not .c or .cpp
    2. use g++ compiler not gcc
    3. use int main() not void main()
    1. .cpp is a valid extension for a C++ program, .c is valid for a C program.
    2. g++ is for C++ code - your case; whereas gcc is for C code - it doesn't understand anything about cout's, cin's etc - they are objects of classes...
    3. shouldn't matter - g++/gcc would just give a warning and not an error - I've always used void main() throughout whenever my program didn't need to return the status back to the shell/OS. The compiler just gives a warning which can be safely ignored
    The Unforgiven
    Registered Linux User #358564

  5. #5
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    .cpp , .cxx , .cc , or .C are all valid c++ file extensions. Could very well be more, but use one of these (.cpp is the most used).

    You can compile c++ with gcc, but using g++ is easier since you don't need to pass a few options.

    The reason main() needs to return int is because when it is all done it will return an int. Returns 0 for exit ok, anything else is up to the coder, but it is always an int.

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    In the C99 standard main must return int.Regarding void main
    gcc may be able to handle it but other compilers may not.I think I've read
    somewhere that some may even create a wrong executable.

  7. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by Santa's little helper
    I think I've read somewhere that some may even create a wrong executable.
    The problem is primarily that libc relies on the returned value from main in order to call _exit correctly to terminate the process.

    If one defines main as a void-returning function, libc will still assume that it is returning an int, and therefore, the returned value that libc reads is undefined (since it didn't return a value ;-) ). On IA32, libc will probably read whatever is currently in the EAX register. On other architectures, the program may crash.

    Therefore, the exit status of the compiled program will be more or less random. That's the problem.

  8. #8
    Just Joined!
    Join Date
    May 2005
    Location
    Perth, Australia
    Posts
    12
    In C++, void main() is generally bad practise,
    Code:
    int main&#40;&#41; &#123;
        ....
        return 0;
    &#125;
    is the more conventional way to do things.

  9. #9
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Therefore, the exit status of the compiled program will be more or less random. That's the problem.
    I don't think a random exit value would cause it to crash because after all
    the programme itself can return any value it wants and it still won't crash.
    My guess is that if the environment expects a return value but the executable
    doesn't provide one (because it was compiled with void main) then either
    the stack pointer will end up having the wrong value (because the environment
    poped out one more value than it should) or the content of some register
    will be interpreted in the wrong manner.Either of these can get it to crash.

  10. #10
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Dolda meant something different.

    The c runtime calls something named _startup to get any C process up and going.

    _startup calls main()

    When main() exits an executable image rundown function is called;
    rundown functions clean up what _startup functions created.

    The value that main returns is of special interest to the rundown function.
    If it gets garbage (ie., random) values, the result can be unpredictable.
    Or the rundown function can crash or segfault.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •