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 hellomake.c , this calls a function called myPrintHelloMake . This function is located in a different source file called hellofunc.c where hellofunc.c also needs an include file, hellomake.h . |
|To compile these three source files by hand to make one executable, myexecutable , you can make each object file, by typing|
gcc -c hellomake.c
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 hellomake.o hellofunc.o
|Or, alternatively, you could even do this build by hand by typing the following on one line:|
gcc hellomake.c hellofunc.c -I. -o myexecutable
|To run your program, at the prompt type:|
|You should get the following output:|
|When you have built and run this executable satisfactorily, you should delete the files: myexecutable, and all files ending in .o|