I have the following source code in main.cpp:
#include
#include
int main() {
std::cout << \"Hi\" << std::en
GCC internally links against a few additional libraries and object files. To see what those are, execute -###, which will print the tool commands it would execute, including the linker. The linker command used by my GCC is (I made the actual .o file I invoked g++ on bold, so you can spot it easily).
/usr/lib/gcc/i686-pc-linux-gnu/4.6.0/collect2 --build-id --eh-frame-hdr -m elf_i386 "--hash-style=both" -dynamic-linker /lib/ld-linux.so.2 /usr/lib/gcc/i686-pc-linux-gnu/4.6.0/../../../crt1.o /usr/lib/gcc/i686-pc-linux-gnu/4.6.0/../../../crti.o /usr/lib/gcc/i686-pc-linux-gnu/4.6.0/crtbegin.o -L/usr/lib/gcc/i686-pc-linux-gnu/4.6.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.6.0/../../.. main1.o "-lstdc++" -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/i686-pc-linux-gnu/4.6.0/crtend.o /usr/lib/gcc/i686-pc-linux-gnu/4.6.0/../../../crtn.o
You can substitute the collect2 path by ld (if I remember correctly, collect2 is only needed as a proxy for the real ld by GCC backends that don't support modern binary formats, to collect constructor and destructor functions according to their mangled name. The ELF format has native sections support for that).
Executing that command, with ld substituted literally, successfully links the executable.