Write a makefile to build the part 1 program (Down). The makefile should contain
ID: 3689724 • Letter: W
Question
Write a makefile to build the part 1 program (Down). The makefile should contain the following rules:
1) Build readline.o by compiling readline.c
2) Build dogs.o by compiling dogs.c
3) Build records.o by compiling records.c
4) Build records by linking readline.o, dogs.o, and records.o
Each rule should include the name of the target file, dependencies among files, and the command to be executed. The makefile should name the executable file for the program records.
Part(1) Already answered; incase you want to know the question asked :
Modify Project 10 dogs.c (Already given )program so that the program is split into three source files and two header files.
1) Put all functions related to operations on the list of dogs into dogs.c
2) Create a header file named dogs.h that contains struct dog declaration and prototypes for the functions in dogs.c. The header file should enclose the contents of the header file in an #ifndef-#endif pair to protect the file.
3) Put the read_line function is in a separate file named readline.c.
4) Create a header file named readline.h that contains a prototype for the read_line function. The header file should enclose the contents of the header file in an #ifndef-#endif pair to protect the file.
5) records.c contains the main function.
6) Include appropriate header files in the source files.
/****************************
*Answer to part 1 *
****************************/
Dogs.h
#ifndef dog_h
#define dog_h
#define NAME_LEN 30
struct dog{
int number;
char dog_name[NAME_LEN+1];
char owner_last_name[NAME_LEN+1];
char breed[NAME_LEN+1];
struct dog *next;
};
/*function prototypes*/
struct dog *append(struct dog *list);
void search(struct dog *list);
void print(struct dog *list);
void clear(struct dog *list);
int read_line(char str[], int n);
#endif
Dogs.c:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include"dogs.h"
struct dog *append(struct dog *list){
struct dog *cur, *new_node;
new_node = malloc(sizeof(struct dog));
if (new_node == NULL) {
printf("Database is full; can't add more dogs. ");
return list;
}
printf("Enter dog's patient number: ");
scanf("%d", &new_node->number);
for (cur = list;cur != NULL;cur = cur->next)
if (cur != NULL && new_node->number == cur->number) {
printf("Patient already exists. ");
free(new_node);
return list;
}
printf("Enter dog's name: ");
read_line(new_node->dog_name, NAME_LEN);
printf("Enter dog's breed: ");
read_line(new_node->breed, NAME_LEN);
printf("Enter owner's last name: ");
read_line(new_node->owner_last_name, NAME_LEN);
new_node->next = NULL;
if(list == NULL)
{
list = new_node;
return list;
}
else{
for(cur = list; cur->next!= NULL; cur = cur->next);
cur->next = new_node;
return list;
}
}
/***********************************************************
* search: Prompts the user to enter a dog's name, then *
* looks up dog(s) by name in the list. Prints the all the *
* informaiton of the dogs with the name if found. *
* Otherwise, prints a message. *
* ********************************************************/
void search (struct dog *list)
{
char search_name[NAME_LEN+1];
struct dog *p;
int found =0;
printf("Enter dog's name: ");
read_line(search_name, NAME_LEN);
for(p=list;
p != NULL;
p = p->next)
{
if(strcmp(search_name, p->dog_name)==0){
found = 1;
printf("%d ", p->number);
printf("%s ", p->dog_name);
printf("%s ", p->breed);
printf("%s ", p->owner_last_name);
}
}
if(!found)
printf("dog not found. ");
}
/************************************************************
* print: Prints a listing of all dogs in the list, showing *
* the dog's patient number, name, breed, and owner's last *
* name. *
* *********************************************************/
void print(struct dog *list){
struct dog *p;
printf("Dog Number Dog Name "
"Dog Breed Owner Last Name ");
for (p = list; p != NULL; p = p->next)
printf("%d %s %s %s ", p->number, p->dog_name,p->breed,
p->owner_last_name);
}
/***************************************************************
* clear: Clears the entire linked list. It begins at the head *
* of the list and frees memory allocated for each node of the *
* linked list. *
* ************************************************************/
void clear(struct dog *list)
{
struct dog *p;
while(list!=NULL)
{
p = list;
list = list->next;
if(p!=NULL)
free(p);
}
}
Readline.h
#ifndef read_lineh
#define read_lineh
#define NAME_LEN 30
int read_line(char str[], int n);
#endif
Readline.c:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include"readline.h"
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
Records.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include"readline.h"
#include"dogs.h"
int main(void)
{
char code;
struct dog *dog_list = NULL;
printf("Operation Code: a for appending to the list, s for finding a dog"
", p for printing the list; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': dog_list = append(dog_list);
break;
case 's': search(dog_list);
break;
case 'p': print(dog_list);
break;
case 'q': clear(dog_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
Explanation / Answer
Dogs.h
#ifndef dog_h
#define dog_h
#define NAME_LEN 30
struct dog{
int number;
char dog_name[NAME_LEN+1];
char owner_last_name[NAME_LEN+1];
char breed[NAME_LEN+1];
struct dog *next;
};
/*function prototypes*/
struct dog *append(struct dog *list);
void search(struct dog *list);
void print(struct dog *list);
void clear(struct dog *list);
int read_line(char str[], int n);
#endif
Dogs.c:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include"dogs.h"
struct dog *append(struct dog *list){
struct dog *cur, *new_node;
new_node = malloc(sizeof(struct dog));
if (new_node == NULL) {
printf("Database is full; can't add more dogs. ");
return list;
}
printf("Enter dog's patient number: ");
scanf("%d", &new_node->number);
for (cur = list;cur != NULL;cur = cur->next)
if (cur != NULL && new_node->number == cur->number) {
printf("Patient already exists. ");
free(new_node);
return list;
}
printf("Enter dog's name: ");
read_line(new_node->dog_name, NAME_LEN);
printf("Enter dog's breed: ");
read_line(new_node->breed, NAME_LEN);
printf("Enter owner's last name: ");
read_line(new_node->owner_last_name, NAME_LEN);
new_node->next = NULL;
if(list == NULL)
{
list = new_node;
return list;
}
else{
for(cur = list; cur->next!= NULL; cur = cur->next);
cur->next = new_node;
return list;
}
}
/***********************************************************
* search: Prompts the user to enter a dog's name, then *
* looks up dog(s) by name in the list. Prints the all the *
* informaiton of the dogs with the name if found. *
* Otherwise, prints a message. *
* ********************************************************/
void search (struct dog *list)
{
char search_name[NAME_LEN+1];
struct dog *p;
int found =0;
printf("Enter dog's name: ");
read_line(search_name, NAME_LEN);
for(p=list;
p != NULL;
p = p->next)
{
if(strcmp(search_name, p->dog_name)==0){
found = 1;
printf("%d ", p->number);
printf("%s ", p->dog_name);
printf("%s ", p->breed);
printf("%s ", p->owner_last_name);
}
}
if(!found)
printf("dog not found. ");
}
/************************************************************
* print: Prints a listing of all dogs in the list, showing *
* the dog's patient number, name, breed, and owner's last *
* name. *
* *********************************************************/
void print(struct dog *list){
struct dog *p;
printf("Dog Number Dog Name "
"Dog Breed Owner Last Name ");
for (p = list; p != NULL; p = p->next)
printf("%d %s %s %s ", p->number, p->dog_name,p->breed,
p->owner_last_name);
}
/***************************************************************
* clear: Clears the entire linked list. It begins at the head *
* of the list and frees memory allocated for each node of the *
* linked list. *
* ************************************************************/
void clear(struct dog *list)
{
struct dog *p;
while(list!=NULL)
{
p = list;
list = list->next;
if(p!=NULL)
free(p);
}
}
Readline.h
#ifndef read_lineh
#define read_lineh
#define NAME_LEN 30
int read_line(char str[], int n);
#endif
Readline.c:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include"readline.h"
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
Records.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include"readline.h"
#include"dogs.h"
int main(void)
{
char code;
struct dog *dog_list = NULL;
printf("Operation Code: a for appending to the list, s for finding a dog"
", p for printing the list; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': dog_list = append(dog_list);
break;
case 's': search(dog_list);
break;
case 'p': print(dog_list);
break;
case 'q': clear(dog_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
A Simple Makefile for the code given:
Here is a straightforward makefile that describes the way an executable file called edit depends on three object files which, in turn, depend on three C source and three header files.
1) Build readline.o by compiling readline.c
2) Build dogs.o by compiling dogs.c
3) Build records.o by compiling records.c
4) Build records by linking readline.o, dogs.o, and records.o
edit : readline.o dogs.o records.o
cc -o edit readline.o dogs.o records.o
readline.o : readline.c readline.h
cc -c readline.c
dogs.o : dogs.c dogs.h
cc -c dogs.c
records.o : records.c
cc -c records.c
clean :
rm edit readline.o dogs.o records.o
We split each long line into two lines using backslash/newline; this is like using one long line, but is easier to read.
To use this makefile to create the executable file called edit, type:
make
To use this makefile to delete the executable file and all the object files from the directory, type:
make clean
In the example makefile, the targets include the executable file ‘edit’, and the object files ‘dogs.o’ and ‘records.o’ ,‘readline.o’. The prerequisites are files such as ‘dogs.c’ and ‘dogs.h’. In fact, each ‘.o’ file is both a target and a prerequisite. Recipes include ‘cc -c dogs.c’ and ‘cc -c records.c’.
When a target is a file, it needs to be recompiled or relinked if any of its prerequisites change. In addition, any prerequisites that are themselves automatically generated should be updated first. In this example, edit depends on each of the eight object files; the object file dogs.o depends on the source file dogs.c and on the header file dogs.h.
A recipe may follow each line that contains a target and prerequisites. These recipes say how to update the target file. A tab character (or whatever character is specified by the .RECIPEPREFIX variable; seeSpecial Variables) must come at the beginning of every line in the recipe to distinguish recipes from other lines in the makefile. (Bear in mind that make does not know anything about how the recipes work. It is up to you to supply recipes that will update the target file properly. All make does is execute the recipe you have specified when the target file needs to be updated.)
The target ‘clean’ is not a file, but merely the name of an action. Since you normally do not want to carry out the actions in this rule, ‘clean’ is not a prerequisite of any other rule. Consequently, make never does anything with it unless you tell it specifically. Note that this rule not only is not a prerequisite, it also does not have any prerequisites, so the only purpose of the rule is to run the specified recipe. Targets that do not refer to files but are just actions are called phony targets.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.