Thread: why gcc can't compile C++

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    why gcc can't compile C++

    This problem was solved and this is a stupid question.

    But I leave my topic here hope this can helps some one.

    hi everyone, im new to programming. I only do web programming before.;

    i tried to compile those line of codes with gcc on my CentOS 5 server:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
      cin.get();
    }
    It gave errors.

    I googled it and people said i should use g++ instead of gcc; Yeah that would be fine but WHY ?

    On this site GCC, the GNU Compiler Collection - GNU Project - Free Software Foundation (FSF) they said gcc can compile c++ as well, why i cannot use it?

    Please somebody let me know why, i will appreciate so much. I hate doing things that i dont know why. I will not use g++ to compile my *.cpp until i know why. Thank you.
    Last edited by namduong; 08-19-2010 at 06:56 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    gcc and g++ are both wrappers around the same underlying compiler engine. They both understand C++ and can compile it. However, the linking step to produce a final executable is different for the C and C++ languages. The gcc wrapper has no knowledge of the C++-specific libraries needed to link a C++ program. Therefore, the link step will fail with some obscure error messages.

    Unless you are compiling a truly standalone C++ program that doesn't use any C++ features which are provided by runtime support (i.e., no exceptions, no C++ standard libraries, no nothing, really), you cannot use gcc to link a C++ program.

    However, you may be able to compile with gcc and then link with g++, though I have no idea why you'd want to:

    Code:
    $ XXXXXXXXXX
    $ XXXXXXXXXX
    I do not recommend the above. In fact, it's so disgusting that I've blanked it out because you'd probably take it and run with it.

    I will not use g++ to compile my *.cpp until i know why. Thank you.
    This is simply baffling. The default assumption should be to use the right tool for the job. There's being curious, and there's being deliberately obstinate.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    thanks brewbuck, so gcc doesn't have c++ libraries and it can't understand anything that belongs to std namespace.

    I think i was confused between "gcc" - the tool to compile and link C, and "GCC" - GNU Compiler Collection; ...

    so g++ came with GCC when i installed GCC on my server. I was wondering where the hell is that g++ came from.

    thanks for your help, i will use g++ to compile my works from now.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    GCC used to stand for Gnu C Compiler. Although it stands for Gnu Compiler Collection now, the name that invokes the C compiler stuck. On a traditional Unix system, would you expect that invoking cc on a Fortran source file would be particularly useful? Why then would you want to use the C compiler on a C++ file? Use g++ - it is analogous to CC on other Unix systems.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    Quote Originally Posted by kermit View Post
    GCC used to stand for Gnu C Compiler. Although it stands for Gnu Compiler Collection now, the name that invokes the C compiler stuck. On a traditional Unix system, would you expect that invoking cc on a Fortran source file would be particularly useful? Why then would you want to use the C compiler on a C++ file? Use g++ - it is analogous to CC on other Unix systems.
    yeah i know, i though "GCC" and "gcc" are the same thing. Thanks man.

    I love this forum people response so fast.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    It's because this is an easy question to respond to. G++ is a C++ compiler. GCC is a C compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. Weird compile errors (gcc, pthread, math)
    By skytreader in forum C Programming
    Replies: 7
    Last Post: 07-20-2010, 11:33 AM
  3. Replies: 4
    Last Post: 09-02-2007, 08:47 PM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. Replies: 4
    Last Post: 11-14-2001, 10:28 AM