tags:

views:

156

answers:

5

Question: Not able to compile a C++ program with gcc compiler.

**oyscs1 /ms/user/a/agrajagr/test/macros 51$ cat info.C**
#include<iostream>
using std::cout;
using std::endl;
int main()
{
#ifdef __cplusplus
cout << "C++ compiler in use and version is " << __cplusplus << endl;
#endif
cout <<"Version is " << __STDC_VERSION__ << endl;
cout << "Hi" << __FILE__ << __LINE__ << endl;
}
**oyscs1 /ms/user/a/agrajagr/test/macros 52$ gcc info.C**
Undefined                       first referenced
 symbol                             in file
cout                                /var/tmp/ccPxLN2a.o
endl(ostream &)                     /var/tmp/ccPxLN2a.o
ostream::operator<<(ostream &(*)(ostream &))/var/tmp/ccPxLN2a.o
ostream::operator<<(int)            /var/tmp/ccPxLN2a.o
ostream::operator<<(long)           /var/tmp/ccPxLN2a.o
ostream::operator<<(char const *)   /var/tmp/ccPxLN2a.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Isn't gcc compiler capable of compiling C++ programs? On a related note, what is the difference between gcc and g++. Thanks,

+6  A: 

use g++ instead of gcc.

Alexandre C.
A: 

If I recall correctly, gcc determines the filetype from the suffix. So, make it foo.cc and it should work.

And, to answer your other question, that is the difference between "gcc" and "g++". gcc is a frontend that chooses the correct compiler.

Borealid
+10  A: 

If you give the code a .c extension the compiler thinks it is C code, not C++. And the C++ compiler driver is called g++, if you use the gcc driver you will have linker problems, as the standard C++ libraries will not be linked by default. So you want:

g++ myprog.cpp

And do not even consider using an uppercase .C extension, unless you never want to port your code, and are prepared to be hated by those you work with.

anon
@Neil: from what I can tell, gcc does consider a `.C` (capital) extension a c++ extension. The following works just fine: `gcc test.C -lstdc++ -o test` or `g++ test.C -o test`.
Evan Teran
@Evan Yes, of course it does. That does not mean that you should ever use it though, as it will cause terrible portability problems, as do all case-sensitive file names. The extension for C++ files should always be .cpp. This causes no problems, and is guaranteed to work on all common operating systems.
anon
+1  A: 

The difference between gcc and g++ are:

     gcc            |        g++
compiles c source   |   compiles c++ source

use g++ instead of gcc to compile you c++ source.

Praveen S
It's a bit more complicated than that, as in the other answers here. gcc will select the correct back-end compiler based on file extension (i.e. will compile a .c as C and a .cc as C++) and links binaries against just the standard C and GCC helper libraries by default regardless of input languages; g++ will also select the correct back-end based on extension except that I think it compiles all C source as C++ instead (i.e. it compiles both .c and .cc as C++) and it includes libstdc++ in its link step regardless of input languages.
Rup
@Rup: correct, this is basically what I was saying in my answer.
Evan Teran
+3  A: 

gcc can actually compile c++ code just fine. The errors you received are linker errors, not compiler errors.

Odds are that if you change the compilation line to be this:

gcc info.C -lstdc++

which makes it link to the standard c++ library, then it will work just fine.

However, you should just make your life easier and use g++.


EDIT:

Rup says it best in his comment to another answer:

[...] gcc will select the correct back-end compiler based on file extension (i.e. will compile a .c as C and a .cc as C++) and links binaries against just the standard C and GCC helper libraries by default regardless of input languages; g++ will also select the correct back-end based on extension except that I think it compiles all C source as C++ instead (i.e. it compiles both .c and .cc as C++) and it includes libstdc++ in its link step regardless of input languages.

Evan Teran