Oh God Why


Note that this is what worked for me on Debian Jessie. It probably won’t work for you.

Write the file
  #include <iostream>

  int main() {
    std::cout << "Hello, World\n";
    return 0;
Run the preprocessor on the file
  $ cpp main.cpp -o main.i

This will take the cpp file, run the preprocessor, and then output the .i file.

Convert the .i file to assembly code
  $ g++ -S -o main.s main.i

gcc with the -S option only converts the preprocessed file into assembly. It outputs it to main.s.

Convert the .s file to an object file.
  $ /usr/bin/as main.s -o main.o
  $ ld                                                                   \
  > -dynamic-linker                                                      \
  > /lib64/ld-linux-x86-64.so.2                                          \
  > -o main                                                              \
  > /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o   \
  > /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crti.o   \
  > /usr/lib/gcc/x86_64-linux-gnu/4.9/crtbegin.o                         \
  > main.o                                                               \
  > -L/usr/lib/gcc/x86_64-linux-gnu/4.9                                  \
  > -lstdc++                                                             \
  > -lc                                                                  \
  > /usr/lib/gcc/x86_64-linux-gnu/4.9/crtend.o                           \
  > /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crtn.o

This ld command will be completely different on a different computer/version of g++. I created it by using g++ -v and copying the command line options for the linker which I actually needed. The above should be totally minimal.

Run the executable!
  $ ./main
    Hello, World

In the words of rubenvb on StackOverflow, “It’s not practical to do this… ever.”

I’d like to thank StackOverflow, manpages, caffeine, and the sheer grit available at 2:00 AM for my completion of this project.

was it worth it? No.