I have the following C++ code in a file called helloworld.cpp
:
#include<iostream>
int main()
{
std::cout << "Hello, World!\n";
}
I would like to compile this manually so I can really understand how the compilation steps work with gcc
, namely:
- Preprocessing
- Compilation
- Assembly
- Linking
This article gives some useful information about how to break up the compilation steps. And so I came up with the following:
Preprocessing
cpp helloworld.cpp > helloworld.i
Compilation
g++ -S helloworld.i
Assembly
as -o helloworld.o helloworld.s
Linking
ld -o helloworld helloworld.o
Everything seems to work except for the last step, as outlined by the article:
ld -o hello hello.o ...libraries...
The libraries argument above is a long list of libraries that you need to find out. I omitted the exact arguments because the list is really long and complicated, and depends on which libraries
g++
is using on your system. If you are interested to find out, you can run the commandg++ -Q -v -o hello hello.cpp
and take a look at the last line whereg++
invokescollect2
And so I tried running g++ -Q -v -o helloworld helloworld.cpp
, but the result is extremely verbose.
I'm still unsure how to complete ld
such that iostream
can be available to the linker when I invoke it. How can I make sure iostream
is available to the linker?