GCC can determine which language a file is in based on the file extension. However, GCC does not automatically link in run time support for any language other than C. In practice that means you can compile C++ programs using gcc
instead of g++
but you'll need to add the -lstdc++
directive:
#include <iostream>
int main()
{
std::cout << "Hello world\n";
}
g++ hello.cc
gcc hello.cc -lstdc++
More accurately, you will need to specify -lstdc++
if you you use the standard library, exceptions, operator new
, or RTTI. For instance, try compiling the following without -lstdc++
:
int main()
{
try {
throw 1;
}
catch (int i)
{
return i;
}
}
Please note that STL containers (including std::string
s) use operator new
by default. Strictly speaking you should be able to use the algorithms (std::min
, std::find_first_of
, etc.) binders and a few other things in the standard library without -lstdc++
but for the most part you might as well include it (the linker will ignore any libraries that you don't actually link to).