Skip to main content Skip to navigation

1. Introduction

Atter an introduction to simple programming (Session 2), you will have experienced how straightforward it is to compile an executable from a single source. However, in scientific computing, it's often the case that you'll be building an executable from several source files. If this is a small number of source files, you could of course just compile them by hand and link them together to make your final executable. You would not want to try this if you had 50 source files to build.

Putting it into Practice:

First try building an executable by hand from several sources.
To do this, download the three source files that are linked on the right-hand side of the page. Don't worry if you don't code in C - that's not the point. You could do the same thing with Fortran or C++
The main source file is hellomain.c, this calls a function called myPrintHelloMake. This function is declared in a different source file called hellofunc.h, and the actual definition resides in hellofunc.c.
To compile these three source files by hand to make one executable, myexecutable, you can make each object file, by typing
 gcc -c hellomain.c -I. 
 gcc -c hellofunc.c -I. 
where the -I. is there to tell the compiler to look for the include file in the current working directory (.)
You can then link these object files to make the final executable by typing
 gcc -o myexecutable hellomain.o hellofunc.o  
Or, alternatively, you could even do this build by hand by typing the following on one line:
 gcc  hellomain.c hellofunc.c -I. -o myexecutable 
To run your program, at the prompt type:
You should get the following output:
 Hello makefiles! 
When you have built and run this executable satisfactorily, you should delete the files: myexecutable, and all files ending in .o

Three source files