5

I have a file named main.cpp which includes iostream.

I compiled main.cpp and it worked without errors, so my question is: I compiled main.cpp and I did not link iostream with main.cpp, so how could this be possible? Or did the compiler linked the iostream automatically?

ezdazuzena
  • 6,120
  • 6
  • 41
  • 71
AlexDan
  • 3,203
  • 7
  • 30
  • 46

2 Answers2

9

The functions in iostream are part of the C++ standard library, which you usually don't need to link explicitly.

If you use a compiler that's not strictly a C++ compiler, you sometimes need to add something like -lstdc++ (at least, I do if I use gcc rather than g++).

James M
  • 18,506
  • 3
  • 48
  • 56
  • so g++ always links c++ standard library with files that containt (e.g iostream )without adding the c++ standart library during the linking time ? – AlexDan Feb 15 '12 at 14:11
  • Yes: you don't need to link libc when you compile C either. It'd be unusually painful for the compiler not to link the language runtime automatically. – Useless Feb 15 '12 at 14:17
0

The iostream library is part of the “compiler”, in the largest sense of the word, and if you invoke the linker through the C++ compiler driver, (g++, cl, etc.), it will be automatically included; IDE's also generally arrange for it to be automatically included. If you invoke the linker directly (ld, link, etc.), then you'll generally have to specify it explicitly. The same thing is true if the compiler driver doesn't understand C++ (the case of gcc).

James Kanze
  • 150,581
  • 18
  • 184
  • 329