With gcc 4.6 when trying to execute this code:
#include
using namespace std;
#include
int main()
{
//Int<> a
The gcc
command is just a driver program that runs the right compiler for the source file you give it, (or run the linker if you give it object files). For a C++ source file with a known file extension it will run the C++ compiler (which for GCC is called cc1plus
) and compile the code correctly.
But it won't automatically link to the C++ Standard Library (which for GCC is called libstdc++
).
To solve the problem you either need to link to that library explicitly by adding -lstdc++
to the options when linking, or alternatively just compile and link with the g++
driver instead, which automatically adds -lstdc++
to the linker options.
So you can use gcc
to compile C++ programs, but if you use any features of the standard library or C++ runtime (including exception handling) then you need to link to the C++ runtime with -lstdc++
(or -lsupc++
for just the runtime). Using g++
does that for you automatically.
This is documented in the manual: https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html