114

How can I compile a C++ program with the GCC compiler?

File 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 the GCC compiler capable of compiling C++ programs? On a related note, what is the difference between gcc and g++?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xyz
  • 8,607
  • 16
  • 66
  • 90

9 Answers9

179

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++.


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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
58

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.

  • @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 Jul 08 '10 at 17:19
  • 5
    @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. –  Jul 08 '10 at 18:28
27

You should use g++ instead of gcc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
16

By default, gcc selects the language based on the file extension, but you can force gcc to select a different language backend with the -x option thus:

gcc -x c++

More options are detailed on the gcc man page under "Options controlling the kind of output". See e.g. gcc(1) - Linux man page (search on the page for the text -x language).

This facility is very useful in cases where gcc can't guess the language using a file extension, for example if you're generating code and feeding it to gcc via standard input.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bleater
  • 5,098
  • 50
  • 48
13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Praveen S
  • 10,355
  • 2
  • 43
  • 69
  • 23
    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 Jul 08 '10 at 17:11
  • 2
    @Rup: correct, this is basically what I was saying in my answer. – Evan Teran Jul 08 '10 at 17:13
3

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
  • 95,191
  • 9
  • 106
  • 122
2

An update with my GCC version 10.3.0 on Ubuntu. Even if I add -lstdc++ or use the correct extension, cc1plus must be installed on the system. Otherwise this error shows up:

gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory

One way to get this is to install g++ even if you're not going to use it directly, e.g. sudo apt install g++.

Now, if I use the extension .cc, I can just call gcc directly, however, warnings and errors still show up. To use gcc cleanly, with a .c extension I have to call it like this:

gcc -x c++ info.c -lstdc++

Shorter if I use the .cc extension, like the accepted answer:

gcc info.cc -lstdc++

Or like others have said, just use g++ info.c instead. No extra parameters are needed to indicate C++, and it works with .c, .cc and .cpp extensions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nagev
  • 10,835
  • 4
  • 58
  • 69
0

It worked well for me. Just one line code on the Windows command line (CMD).

First, confirm that you have installed gcc (for C) or g++ (for C++) compiler.

On the command line, for gcc, type:

gcc --version

On the command line, for g++, type:

g++ --version

If it is installed then proceed.

Now, compile your .c or .cpp using the command line.

For C syntax:

gcc -o exe_filename yourfilename.c

Example:

gcc -o myfile myfile.c

Here exe_filename (myfile in example) is the name of your .exe file which you want to produce after compilation (Note: I have not put any extension here). And yourfilename.c (myfile.c in example) is the your source file which has the .c extension.

Now go to the folder containing your .c file. Here you will find a file with the .exe extension. Just open it. Hurray...

For C++ syntax:

g++ -o exe_filename yourfilename.cpp

After it, the process is the same as for the C syntax.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajkumar Bansal
  • 323
  • 2
  • 10
0

For a .cpp File:

g++ myprog.cpp -o myprog
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Augusta
  • 9
  • 1
  • 1
  • 3