问题
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?
回答1:
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++).
回答2:
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).
来源:https://stackoverflow.com/questions/9294749/c-linker-how-to-link-the-iostream-file