AlexDan
AlexDan

Reputation: 3351

c++ linker , how to link the iostream file?

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?

Upvotes: 5

Views: 7417

Answers (2)

James Kanze
James Kanze

Reputation: 154017

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

Upvotes: 0

James M
James M

Reputation: 16728

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

Upvotes: 9

Related Questions