Tutorial: Compiling C++ code

Anyone knows a simple tutorial on how to compile a C++ code? Ideally in the following order of complexity:

  1. Compile & link from one source file directly

  2. Compile & link multiple source files directly

  3. Introducing Makefile to help manage the complexity of compiling multiple source

  4. It goes on and on (automake, autoconf … opps, perhaps not this far)

Assume the person who needs this tutorial has only limited experience of building a program (i.e. through a “build” button on IDE, never on the CLI).

Wirawan

Hi Wirawan,

We’re doing one of those here for all compiled languages in a month. I’m starting work on the materials this week. I can send you what we come up with. Need at least configure to tell them they need to use --prefix but that’s as far as we’ll go with those tools.

Ancient documentation, but still usable:

Single-file compiles: https://www.cae.tntech.edu/help/programming/compiling

Makefiles: https://www.cae.tntech.edu/help/programming/make_tutorial (now with a dead link to a U Hawaii page with additional information, the archive.org mirror of it is at https://web.archive.org/web/20110615161204/https://www.eng.hawaii.edu/Tutor/Make/)

I’ve got nothing for autotools.

Tutorial on Makefiles with C examples: https://docs.google.com/document/d/1fzIlFP8T3AkoeIk2Cm9nN11S0M5VSROf9DrqI8GGzDE

Software carpentry has a lesson on Makefiles. http://swcarpentry.github.io/make-novice/

Also on using the Unix shell. https://software-carpentry.org/lessons/

1 Like

Here is nice intro to compiling and linking in c++ : https://github.com/green7ea/cpp-compilation

Here is a small detour towards understanding how shared libraries work : https://amir.rachum.com/blog/2016/09/17/shared-libraries/

Finally, spack has an excellent overview of multiple build systems for a large variety of programming languages here : https://spack.readthedocs.io/en/latest/build_systems.html