PHYS 105 Lecture 14: Large Programs and
Makefiles
Sean Fleming
Dept. of Physics
University of Arizona
November 27 & 28
Last time
I Orbital Motion
This time
I Large Programs
I Makefiles
Large Programs
As you may have noticed, our programs are growing ever
larger. This becomes a problem if you want an individual file to
be readable. In general, it is good coding philosophy to break a
large file into smaller pieces that all do a self-contained
operation. This is the philosophy we will take in breaking apart
some of our larger programs.
Breaking apart gaussian.c
Today, we will break apart the gaussian.c program that we
wrote way back when. Feel free to copy/paste into a new file,
but I will be writing the code from scratch.
Our strategy will be to put the gaussian function, previously
written just below the main(), into its own file.
Header files
The way you link c-files together is through “header” files.
These files contain the prototypes of the functions found in an
identically named c-file. For this program, we will create a
header called “function.h” for the c-file called “function.c”.
function.h
This file, called “function.h” contains the prototype and library
links used in the code with the same name but a “.c” suffix.
//Function prototypes are put in headers
//As well as inclusions of libraries
#include <math.h>
float gaussian(float x);
function.c
The function itself is in a c-file.
//Link back to the header
#include "function.h"
float gaussian(float x){
return exp(-x*x);
}
gauss_main.c
The entire main is in its own file.
//Tom McClintock
//PHYS 105
//to compile: $make
//to run: $gaussian.exe
#include <stdio.h>
//Include your own header files here!
#include "function.h"
int main()
{
int i, N = 20;
float xLo = -5, xHi = 5,x = xLo, y;
float step = (xHi-xLo)/N;
for(i = 0; i < N; i++){
y = gaussian(x);
printf("x = %f y = %e\n",x,y);
x = x + step; //increment x
}
}
Superglue: the Makefile
As you may have noticed, the compilation instructions are
different. For programs with many c-files and headers, it is a
pain to compile at the command line. For this reason, we make
a Makefile. A Makefile is like a recipe: it contains the
instructions on how to make our executable.
Makefile
#Name the executable
EXEC = gaussian.exe
#List all of the "objects" you will compile
OBJS = gauss_main.o function.o
#Name your c-ccompiler
CC = gcc
#Link to any libraries
OFLAGS = -lm
.SUFFIXES:.c.o
%.o: %.c
$(CC) -c $< -o $@
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(EXEC) $(OFLAGS)
rm $(OBJS)
Compiling and running
To compile a program with a Makefile all you have to do is type
“make”. To run you just type the name of the executable.
Final Project
I Due on “final” date: Tu. Dec. 11, W. Dec. 12
I In class assignment: Work on your final project.
Next time
I Python programs
Work on your final project!
HW 7 - Harmonic Motion due in one week
Simulate a ball hanging from the ceiling by a spring, and include
air resistance. The force for this would look like the following:
Fy = mg bv 2
vy
v
ky
where F~ always points in the y direction. Here, y is the
displacement from the origin. Simulate this with initial
conditions y(0) = +1.0 and v(0) = ?1.0, with constants of
b = 0.1 and k = 20.0. You can quickly write this homework by
modifying your projectile motion program.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。