Question: How to compile a C++ program with gcc compiler?
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;
}
and when I try to compile info.c
$ 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,
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:
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: